Skip to content Skip to sidebar Skip to footer

Google Maps Api Function Does Not Return Results In Time

So I have an array of objects with the following properties: Address, Latitude, Longitude, and Title poi('139 Main St', 0, 0, 'Farmer's Market' ), poi('18 N Hopkins Ave', 37.4455

Solution 1:

you can solve your issue with function closure. Also, you shouldn't use undocumented properties of the Google Maps Javascript API v3 (.k, .B), they will change with updates of the API and break your code.

var geocoder = new google.maps.Geocoder();
function geocodeAddress(curr, i) {
  geocoder.geocode( { 'address': curr.address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
       cur.lat = results[0].geometry.location.lat();
       cur.long = results[0].geometry.location.lng();
    } else {
       console.log('failed to include ' + curr.title);
       poiArray.splice(i--, 1);    
    }
  });  
}
for (i=0; i < poiArray.length; i++ ) {
    var curr = poiArray[i];
    if ( !curr.lat || !curr.long ) {
        geocodeAddress(curr, i);
    }
}

Post a Comment for "Google Maps Api Function Does Not Return Results In Time"