How to calculate driving distance between two address

Js

Posted on 10 Dec 15 by Vickey
  1700



By using google api we can calculate driving or highway distance which is connecting  two address or in other word we can find the distance between two address for travel

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry"></script>
<script>
getDistance('city1','city2');
function getDistance(addressA, addressB)
{
    if (addressA == "" || addressB == "") {
        return false;
    }

    var origin = addressA;
    var destination = addressB;
    service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
        origins: [origin],
        destinations: [destination],
        travelMode: google.maps.TravelMode.DRIVING,
        avoidHighways: false,
        avoidTolls: false
    },
    callback 
    );
}
function callback(result, status) {

	if (status == "OK") {
		if (result.destinationAddresses[0] == "") {
		    alert("Address first  not found");
		    return false;
		}
		if (result.originAddresses[0] == "") {
		    alert("Address second not found");
		    return false;
		}
		var distanceInKM = result.rows[0].elements[0].distance.text;
		var distanceInMiles = parseInt(distanceInKM) * .6214;
		alert(distanceInMiles);
	}
}
</script>

 


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com