Abhisek says:
//*********DISTANCE AND DURATION**********************//
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [source],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function (response, status) {
if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
var distance = response.rows[0].elements[0].distance.text;
var duration = response.rows[0].elements[0].duration.text;
var dvDistance = document.getElementById("dvDistance");
dvDistance.innerHTML = "";
dvDistance.innerHTML += "Distance: " + distance + "<br />";
dvDistance.innerHTML += "Duration:" + duration;
} else {
alert("Unable to find the distance via road.");
}
});
Replace the above with the below code.
//*********DISTANCE AND DURATION**********************//
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [source],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function (response, status) {
if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
var distance = response.rows[0].elements[0].distance.text;
var duration = response.rows[0].elements[0].duration.text;
if (duration.indexOf('day') != -1) {
var days = duration.split('day')[0].trim();
var hours = duration.split('day')[1].trim().split(' ')[0].trim();
duration = (parseInt(days) * 60 * 24) + (parseInt(hours) * 60) + " mins";
}
else if (duration.indexOf('hours') != -1) {
var hours = duration.split('hours')[0].trim();
var minutes = duration.split('hours')[1].trim().split(' ')[0].trim();
duration = (parseInt(hours) * 60) + parseInt(minutes) + " mins";
}
else if (duration.indexOf('hour') != -1) {
var hour = duration.split('hour')[0].trim();
var minutes = duration.split('hour')[1].trim().split(' ')[0].trim();
duration = (parseInt(hour) * 60) + parseInt(minutes) + " mins";
}
else {
duration = response.rows[0].elements[0].duration.text.trim();
}
var dvDistance = document.getElementById("dvDistance");
dvDistance.innerHTML = "";
dvDistance.innerHTML += "Distance: " + distance + "<br />";
dvDistance.innerHTML += "Duration:" + duration;
} else {
alert("Unable to find the distance via road.");
}
});