Skip to content

Instantly share code, notes, and snippets.

@Jerl92
Created January 14, 2026 10:08
Show Gist options
  • Select an option

  • Save Jerl92/efee78cd586a8770f7843e10f38fe3a0 to your computer and use it in GitHub Desktop.

Select an option

Save Jerl92/efee78cd586a8770f7843e10f38fe3a0 to your computer and use it in GitHub Desktop.
Get distance from two points
// 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();
}
});
}
@Jerl92
Copy link
Author

Jerl92 commented Jan 14, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment