hpneo / gmaps

the easiest way to use Google Maps
https://hpneo.dev/gmaps/
7.03k stars 1.29k forks source link

Problem geocoding and placing markers for multiple addresses #533

Open veeeev opened 7 years ago

veeeev commented 7 years ago

First, this is a great library! i'm just getting started with it but so far it's wonderful!

i've run into an issue regarding geocoding and marker placement that i'm hoping someone can help with.

i'm trying to loop through an array of addresses, geocoding and placing a marker for each.

My code manages to map 11 addresses but not the full 132 i'm currently trying to map.

If i add an "alert" on each loop, creating a temporary pause, i can get all addresses mapped. However, when the loop runs as shown below, only 11 addresses are mapped.

Any help would be appreciated. Thanks in advance!

var myMap = new GMaps({
    div: "#myMapPlace",
    lat: 41.2216838,
    lng: -78.2856733,
    zoom: 12
})

for (i = 0; i < x.length; ++i) {
        GMaps.geocode({
            address: x[i][1],
            callback: function(results, status) {
                    if (status == 'OK') {
                        var latlng = results[0].geometry.location;
                                myMap.createMarker({
                        lat: latlng.lat(),
                        lng: latlng.lng()
                                })
                       }
                    }
        });
}
teknosains commented 6 years ago

+1