I am not experienced in javascript, therefore I do not know much about it!
I make multiple calls to the google API service google.maps.DistanceMatrixService.getDistanceMatrix()
as follows:
for (i=0;i<n;i++) {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: destinations[i],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
So I create several service requests. When those requests are done, the callback
function is called. I assume, this can happen in a different order.
So when the callback
function is called with distance informations, how do I know if the results correspond to destionations[0]
? Or destinations[1]
...? Unfortunately, the location data given in the responses is a string denoting the destination place, like 'Bonigerweg, 4852 Rothrist, Schweiz' instead of a google.maps.LatLng
-object. And here I do not want to call the geocoder
service to back-transform the address to a google.maps.LatLng
-object.
Is there some sane way to solve my problem? Or should I use timing? Make the next call to google.maps.DistanceMatrixService.getDistanceMatrix()
only if the response to the first call has been received?