Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tutorial on how to calculate driving distance using Google maps API, full post available @ http://christianvarga.com/how-to-calculate-driving-distance-between-2-locations-with-google-maps-api/
var directionsService = new google.maps.DirectionsService();
var request = {
origin : 'Melbourne VIC', // a city, full address, landmark etc
destination : 'Sydney NSW',
travelMode : google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if ( status == google.maps.DirectionsStatus.OK ) {
alert( response.routes[0].legs[0].distance.value ); // the distance in metres
}
else {
// oops, there's no route between these two locations
// every time this happens, a kitten dies
// so please, ensure your address is formatted properly
}
});
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
@prineshaz

This comment has been minimized.

Copy link

commented Dec 29, 2017

Thank you!!

@sibghatullahshah

This comment has been minimized.

Copy link

commented Jan 23, 2018

Thank you sir, can we write this distance over marker on that map ?

@BrianWekesa

This comment has been minimized.

Copy link

commented Feb 15, 2018

verry usefull just like @sibghatullahshah requested I will be gratefull to see how that can be done also thanks

@ghost

This comment has been minimized.

Copy link

commented Feb 26, 2018

Thank you, works fine !

@meetgerry

This comment has been minimized.

Copy link

commented May 26, 2018

Can we do this with multiple points? Do you have a script like this?

@NaiyaShah-BTC

This comment has been minimized.

Copy link

commented Jun 11, 2018

Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.