Google Maps API direct Distance

Javascript Direct Distance function for Google Maps API

coordinates format:
decimal coordinates

input values:
lat1: latitude of the first point
lon1: longitude of the first point
lat2: latitude of the second point
lon2: longitude of the second point

return value:
direct distance in kilometers

Javascript Code:
// direct distance
function direct_distance(lat1, lon1, lat2, lon2){
lng = 0; // preset default value
polyPoints = [new GLatLng(lat1,lon1),new GLatLng(lat2,lon2)]; // make points array
polyShape = new GPolyline(polyPoints); // make a line from to points (2 point array)
var lng = polyShape.getLength()/1000; // get length of the line in +- km distance
return lng.toFixed(2); // format output by to number after dot
}

requirements:
google maps api

Notes:
The distances is calculated as a straight line. Also there are some more dificult examples on the Internet how to calculate “straight” distance on a perfect elipsoid.
This is just one of ways how it can be done. You can use polyPoints array as input value if you need…
More information about it on Google Code directory.

Code Taken From: www.atstumai.lt
Other useful website: www.distancestool.com/

One Comment

  1. MaGnet`as says:

    Hmm…everything looks so nice and simple but it still needs some thinking and practice…

Leave a Reply