Skip to content

Instantly share code, notes, and snippets.

@surahmans
Forked from levymetal/directions.js
Created January 30, 2018 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surahmans/f7f8cf90435b563b4709d88d7b79b483 to your computer and use it in GitHub Desktop.
Save surahmans/f7f8cf90435b563b4709d88d7b79b483 to your computer and use it in GitHub Desktop.
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment