Showing posts with label google map. Show all posts
Showing posts with label google map. Show all posts

Friday, April 4, 2014

Get distance between two place in google map

Here I am postting little code for get distance between two place in google map.
We need only latitude and longitude of both place and using this we can get by-road distance between them.

var currentLocation = new google.maps.LatLng(currLatitude,currLongitude);
var service = new google.maps.DistanceMatrixService();
var distance = 0;

var destinationLocation = new google.maps.LatLng(destiLatitude,destiLongitude); service.getDistanceMatrix({
origins: [currentLocation],
destinations: [destinationLocation ],
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false
},
function( response, status ) {
    if (status == google.maps.DistanceMatrixStatus.OK ){
distance = response.rows[0].elements[0].distance.value ;
    }
});

Here you get distance in km.
If you want to convert it into miles you can use below line.

var distanceInMiles = Math.round(( (response.rows[0].elements[0].distance.value /1609.34)*100)/100);