I'm trying to make a Async call to Google Maps for address inside a foreach loop.
This is the function:
//Function to get address from geographic coordinates
function getAddress(item) {
var address = 'Not fetched';
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(parseFloat(item.Latitude), parseFloat(item.Longitude));
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
address = results[0].formatted_address;
});
return address;
}
This is the loop:
recreateMarkers: function (self, data) {
data.vehicleInfo.forEach(function (item) {
self.Location= getAddress(item);
});
}
Structure of Data: 1. VehicleId 2. Latitude 3. Longitude 4. Location
Now the problem is it gives me same or undefined location for all vehicles.
Would be glad if someone can assist.