Geocoding of the addresses array?

get an array of addresses that need geocoding to get the coordinates and add a new array with the coordinates already
smells callbackhellom
how to make

for (var i = 0; i < items.length; i++) {
 var item = items[i];
GMaps.geocode({
 address: item.address+" "+item.city+" "+item.zip,
 callback: function(results, status) {
 if (status == 'OK') {
 var latlng = results[0].geometry.location;
 map.setCenter(latlng.lat(), latlng.lng());

markers_data.push({
 id: item.id
 lat : latlng.lat(),
 lng : latlng.lng(),
 title : item.name
 icon : {
 size : new google.maps.Size(32, 32),
 url : icon
},
 infoWindow: {
 content : "
}
});
}
}
 });


after all are geo-coded - need to call a General colback and add to the map all markers in one call

in addition I have inside of Kabaka GMaps.geocode callback is not available objek item

help
July 2nd 19 at 13:41
2 answers
July 2nd 19 at 13:43
Solution
If you do not want to drag async, it is possible something like this:
var pro = new Promise(function(resolve, reject){
 //...your code
 callback: function(results, status) {
 if (status == 'OK') {
 // test for number of records in markers_data or the last iteration of the loop
 //if so then resolve(markers_data)
}
}
//...
}).then(function(markers_data){
//...
})

If you do not want to use promises that callbackhell Wellcome!
And item should be visible...

Well, you can still have the event to throw. To use any EventEmitter, window well, or at...
item visible, but in each kolbet is the last element of the array. those I get 10 Kabakov but I don't know what each coordinate assinity - erna_Bergstr commented on July 2nd 19 at 13:46
: this is a classic - gustave_Kertzmann52 commented on July 2nd 19 at 13:49
Aah... I realized nedognal)
Wrap it in a function that should help - brycen_Ledner16 commented on July 2nd 19 at 13:52
: Thank you super - erna_Bergstr commented on July 2nd 19 at 13:55
July 2nd 19 at 13:45
It seems async.map just for this

If the task one and the whole library is not needed, the analog async.the map is fairly simple.

BTV, caolan seems to be no promises to cope with this, in the simplest implementation somehow
so
var list_of_items_to_process = [ 1, 2, 3, 4, 5 ];

function asyncMap(list, exec, callback) {
 var processed = 0;

 list.forEach(function(current, index, array) {
 exec(current, index, array, innerCallback.bind(index));
})

 innerCallback function(result) {
 list[this] = result;
processed++;
 if (processed === list.length) {
callback(list);
}
}
}

processor function(value, index, array, callback) { 
 var result;
 setTimeout(function() { 
 result = value*Math.the round(Math.random()*10);
console.log(result)
 callback(result); 
 }, 500*value); 
}

function mainCallback(values) {
console.log(values);
}

asyncMap(list_of_items_to_process, processor, mainCallback)


If I did not mess up, then after you build items, you simply execute
it
function asyncMap(list, exec, callback) {
 var processed = 0;

 list.forEach(function(current, index, array) {
 exec(current, index, array, innerCallback.bind(index));
})

 innerCallback function(result) {
 list[this] = result;
processed++;
 if (processed === list.length) {
callback(list);
}
}
}

processor function(value, index, array, cb) {
GMaps.geocode({
 address: value.address + "" + value.city + "" + value.zip,
 callback: function(results, status) {
 if (status == 'OK') {
cb(results);
 } else {
cb(false);
}
}
});
}

function mainCallback(values) {
 values.forEach(function(value) {
 if (!value) return;

 var latlng = results[0].geometry.location;
 map.setCenter(latlng.lat(), latlng.lng());

markers_data.push({
 id: value.id
 lat: latlng.lat(),
 lng: latlng.lng(),
 title: value.name
 icon: {
 size: new google.maps.Size(32, 32),
 url: icon
},
 infoWindow: {
 content: "
}
});
});
}

asyncMap(items, processor, mainCallback)

Find more questions by tags JavaScriptGoogle Maps