I have created a simple jsfiddle here using Google's Map Distance Matrix to determine the mileage between 2 points. This worked fine, but then I was interested in determining the mileage between more than 2 points. However I could not get very far with this, as I don't think I have implemented it correctly. Does anyone know how to add waypoints to Google's Distance Matrix in order to determine the best route/mileage between them by car? For example if I wanted to get from the The Underpass, Birmingham
to 8 Shandon Close, Birmingham
through a bunch of other places. My code has been included below:
var origin = "The Underpass, Marston Green, Birmingham, West Midlands B40 1PA, UK",
destinations = ["8 Shandon Close, Birmingham, West Midlands B32 3XB, UK","2 The Osiers, Elford, Tamworth, Staffordshire B79 9DG, UK","170 Bells Lane, Birmingham, West Midlands B14 5QA, UK","246 Long Acre, Birmingham, West Midlands B7 5JP, UK","28 Cuthbert Road, Birmingham, West Midlands B18 4AG, UK"]
service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [origin],
destinations: destinations,
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false,
unitSystem: google.maps.UnitSystem.IMPERIAL
},
callback);
function callback(response, status) {
var orig = document.getElementById("orig"),
dest = document.getElementById("dest"),
dist = document.getElementById("dist");
if (status == "OK") {
orig.value = response.originAddresses[0];
dest.value = response.destinationAddresses[0];
dist.value = response.rows[0].elements[0].distance.text;
} else {
alert("Error: " + status);
}
}
See Question&Answers more detail:os