Nel

Nel

  • NA
  • 716
  • 1.1m

Create the origin list properly for DistanceMatrixService()

Aug 17 2014 5:28 PM

Hello,

I have this code where I define the lists for origins and destinations in order to calculate the travel time between the pair. But the way I created the lists or something else is not ok, and this code doesn't perform the calculations.

Can anybody help me where I make a mistake?
Thanks 

origin = origin1 + "," + origin2 + "," + origin3;

destin = destinationA + "," + destinationB + "," + destination;



alert(origin);

  

alert(destin);

function calculateDistances() {

// for (var i = 0; i <= el.length; i++) {

var service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix(

{

origins: [origin], //[origin1, origin2, origin3]

destinations:[destin], //[destinationA, destinationB, destinationC]

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 = document.getElementById('outputDiv');

outputDiv.innerHTML = '';

deleteOverlays();

for (var i = 0; i < origins.length; i++) {

var results = response.rows[i].elements;

addMarker(origins[i], false);

alert(results[i].duration.text);

// for (var j = 0; j < results.length; j++) {

addMarker(destinations[i], true); //was j

outputDiv.innerHTML += origins[i] + ' to ' + destinations[i] //was destination[j]

+ ': ' + results[i].distance.text + ' in ' //results[j].distance.text

+ results[i].duration.text + '<br>'; //results[j].distance.text

//}

}

}

}


Answers (6)