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.
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 + '
'; //results[j].distance.text
//}
}
}
}
NelPosted Aug 20, 2014, 4:39 PM
Hi Sumit,
But can you please write an example with this using the origins and destinations I posted?
Thank you very much
Guest UserPosted Aug 20, 2014, 8:26 AM
my belief is that Google API is not accepting more than requirement number of origins & destinations as desired. So limit it upto what Google example suggest and make it dynamic the way I suggested at top. If you have more origin & destination then i suggest to loop it, e.g.,
_.each(array1, index, function(a){
var origin1 = new google.maps.LatLng(a, a[index+1]);
});
I constructed above loop from Underscore.js you can use the regular FOR loop of JavaScript
Please click checkbox "This is correct answer" if you find it helpful!
NelPosted Aug 20, 2014, 8:21 AM
The first attempt was just yours, the same example from Google developers site But I need dynamically created list I can keep it like this and it works, but I wanted to use an array or similar to solve it in more elegant way
But thanks for your idea anyway, and for your try to help
Guest UserPosted Aug 20, 2014, 6:48 AM
can you try creating your origin & destination pairs like above rather having so many
NelPosted Aug 19, 2014, 1:34 PM
var origin1 = new google.maps.LatLng(26.368628, -80.137214);
var origin2 = new google.maps.LatLng(26.368311, -80.131249);
var origin3 = new google.maps.LatLng(26.368215, -80.128964);
var origin4 = new google.maps.LatLng(26.368099, -80.125042);
var origin5 = new google.maps.LatLng(26.368176, -80.120965);
var origin6 = new google.maps.LatLng(26.367907, -80.116363);
var origin7 = new google.maps.LatLng(26.367475, -80.114678);
var origin8 = new google.maps.LatLng(26.365442, -80.106143);
var destinationA = new google.maps.LatLng(26.368311, -80.131249);
var destinationB = new google.maps.LatLng(26.368215, -80.128964);
var destinationC = new google.maps.LatLng(26.368099, -80.125042);
var destinationD = new google.maps.LatLng(26.368176, -80.120965);
var destinationE = new google.maps.LatLng(26.367907, -80.116363);
var destinationF = new google.maps.LatLng(26.367475, -80.114678);
var destinationG = new google.maps.LatLng(26.365442, -80.106143);
var destinationH = new google.maps.LatLng(26.362884, -80.098408);
var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';
var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';
function initialize() {
var opts = {
center: new google.maps.LatLng(26.368049, -80.116693),
zoom: 15
};
map = new google.maps.Map(document.getElementById('map-canvas'), opts);
geocoder = new google.maps.Geocoder();
}
function calculateDistances() {
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1, origin2, origin3, origin4, origin5, origin6, origin7, origin8],
destinations: [destinationA, destinationB, destinationC, destinationD, destinationE, destinationF, destinationG, destinationH],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
But instead of [origin1, origin2, origin3, origin4, origin5, origin6, origin7, origin8], I thought maybe there is some more intelligent way
Guest UserPosted Aug 19, 2014, 12:38 PM
can you please paste your values origin1 to origin3 and for destination variables.