I am trying to figure out a way of initiating a google distancematrix call where i need to have the result returned before I can carry on with my program execution, using javascript and jquery. Please if anyone can help code sample idea. I have multiple legs for distances. ie: leg 1 start and finish point leg 2 start and finish point leg 3 start and finish point what i am trying to do is calc the travel time between the different legs, so leg 1 finishes driver needs to get to start of leg 2. so doing a call to get the distance between leg 1 finish and leg 2 start. Then moving on to the next calc. I already have the leg distances so, my code is as follows:
var driving = 0;
calculateDistances();
code to work with the calculatedistances() result here
function calculateDistances() {
//alert(origin+','+destination);
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
//set origin and destination points
origins: [origin],//, origin2],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = '';
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
//addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
driving = parseFloat(results[j].distance.text); // driving is a script wide variable to hold the return value
}
}
}
}