Created
January 14, 2026 10:08
-
-
Save Jerl92/efee78cd586a8770f7843e10f38fe3a0 to your computer and use it in GitHub Desktop.
Get distance from two points
This file contains hidden or 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
| // Source - https://stackoverflow.com/a | |
| // Posted by James | |
| // Retrieved 2026-01-14, License - CC BY-SA 3.0 | |
| function getDistance() | |
| { | |
| //Find the distance | |
| var distanceService = new google.maps.DistanceMatrixService(); | |
| distanceService.getDistanceMatrix({ | |
| origins: [$("#autocompleteDeparture").val()], | |
| destinations: [$("#autocompleteArrival").val()], | |
| travelMode: google.maps.TravelMode.WALKING, | |
| unitSystem: google.maps.UnitSystem.METRIC, | |
| durationInTraffic: true, | |
| avoidHighways: false, | |
| avoidTolls: false | |
| }, | |
| function (response, status) { | |
| if (status !== google.maps.DistanceMatrixStatus.OK) { | |
| console.log('Error:', status); | |
| } else { | |
| console.log(response); | |
| $("#distance").text(response.rows[0].elements[0].distance.text).show(); | |
| $("#duration").text(response.rows[0].elements[0].duration.text).show(); | |
| } | |
| }); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/38159768/using-javascript-to-query-google-distance-matrix-api