Last active
November 1, 2022 02:38
-
-
Save levymetal/5083949 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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> |
Thank you sir, can we write this distance over marker on that map ?
verry usefull just like @sibghatullahshah requested I will be gratefull to see how that can be done also thanks
Thank you, works fine !
Can we do this with multiple points? Do you have a script like this?
Thank you!!
Hi Levymetal, With the API now requiring a key how would I modify the code to include that? I dig the username BTW.
I am not much of a programmer but would like to include this functionality in my website.
@GavinWatts you should be able to do something like this with the script tag:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=YOUR_API_KEY"></script>
Give that a try and let me know how you go!
Thank you, sir !!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!!