1

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
     }
    }
}

}

1
  • The google distance call should have an async callback, you place a function their that continues the rest of your program flow. Without any code it is hard to give more specifics, can we see your code?
    – MartinWebb
    Commented Aug 13, 2014 at 8:10

1 Answer 1

1

Pass a callback into your calculateDistances() function, you can do it by passing in callback, and executing it with callback() at the end of the method. Call your function something else for clarity.

var driving = 0;
calculateDistances(callbackFunction);

function calculateDistances(callback) {

 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 callbackFunction(response, status) {
  if (status != google.maps.DistanceMatrixStatus.OK) {
}
//etc
}
5
  • my apologies if i am a bit slow with this, calculatedistances allready has a callback being returned from googles response, Commented Aug 13, 2014 at 8:32
  • is this a custome callback on top of the other - function calculateDistances(callback)? Commented Aug 13, 2014 at 8:33
  • If I understand correctly, there is already a callback, but the calculateDistances() function is asynchronous in your example, so the code in place of "code to work with the calculatedistances() result here" will get run before the results are returned, this way you handle code when the result comes in. Commented Aug 13, 2014 at 8:49
  • not getting the result through to the second callback, the callback does trigger but i cannot see the value i set DRIVING, within the original callback. Commented Aug 13, 2014 at 9:11
  • Thank You for all the effort guys.i have decided to implement this structure in a slightly different way. Commented Aug 13, 2014 at 10:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.