Hi,
I am using the JavaScript GoogleMaps API to map out directions. A
user enters an address and the code cycles through calling for
distance from the address to each of 8 locations. Then it sorts the
locations in order of distance, displaying the closest to the user's
inputted address first.
The issue I have is that I want to store the distance from the address
to each location in the Locations array to be sorted on later (and
some other stuff). The problem I am having is that the for-loop keeps
cycling through and the GoogleMap API call does not return until the
end of the loop, thus, I keep assigning the returned distance to the
last array element everytime, overwriting it. I end up with only the
last element having a value, the last value returned.
Is there a way to make the call a synchronous call so my code stops
executing until the GoogleMaps call returns?
Thanks for any help.
[CODE]
function calcClosest() {
var distances = new Array(7);
document.getElementById('directions_panel').innerHTML = "";
document.getElementById('sites').innerHTML = "<img src = img/
calculating.gif>";
var start = document.getElementById("address").value;
for (instance in Locations) {
end = Locations[i][0] + " " + Locations[i][1];
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
dist =
parseFloat(response.routes[0].legs[0].distance.value);
Locations[instance][3] = Math.round(dists *
0.000621371192237334 *
10)/10;
} else {
alert(status);
}
});
}
setTimeout("calculate()", 5000);
}
[/CODE]
--
You received this message because you are subscribed to the Google Groups
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-js-api-v3?hl=en.