I have successfully implemented the Google Matrix API to retrieve time and distance information from location A to location B.
var origins = [Task.getItem("Y") + "," + Task.getItem("X")];
var destinations = [LAT + "," + LNG];
var distanceMatrix = new google.maps.DistanceMatrixService();
var distanceRequest = {
origins: origins,
destinations: destinations,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
};
distanceMatrix.getDistanceMatrix(distanceRequest, function(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('An error occurred: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
if (response.rows[0].elements[0].distance != null) {
totalDistance = response.rows[0].elements[0].distance.value;
totalTime = response.rows[0].elements[0].duration.value;
ratioPerOneMeter = totalDistance / totalTime;
PRDifference = 0;
updateDistanceAndTime();
} else {
alert("Unable to calculate Distance And Time");
}
}
});
My next step is to add the API key, but I am unsure of how to do so. I tried adding the API key in the script tag:
<script src="http://maps.googleapis.com/maps/api/js?key={KEY}&v=3.exp">
as suggested on Using a Google Apps API key with Distance Matrix
However, I believe this may only be for the map and not the matrix API. Can someone confirm if this approach is sufficient?