javascript
or jquery
?Ok so you can calculate distance between two points in Google maps using Javascript, by using built-in google maps function or you can create it of your own
Solution 1(built-in)
There is the
computeDistanceBetween(lat,lng) in the new
V3 Geometry Library
code for it would be
var latitude1 = 40;
var longitude1 = 0.36;
var latitude2 = 41;
var longitude2 = 3.68;
var TotalDistanceInMeters= google.maps.geometry.spherical.computeDistanceBetween
(new google.maps.LatLng(latitude1, longitude1), new google.maps.LatLng(latitude2, longitude2));
Solution 2 (Create your own function):
var rad = function(x) {
return x * Math.PI / 180;
};
var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s radius in meter
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d; // return distance in meter
};
//Call it like
var loc1 = new GLatLng(52.5773139, 1.3712427);
var loc2 = new GLatLng(52.4788314, 1.7577444);
getDistance(loc1,loc2)
That's it, but I think solution 1 is better.
var rad = function(x) {
return x * Math.PI / 180;
};
var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s radius in meter
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d; // return distance in meter
};
//Do not use this
//var loc1 = new GLatLng(52.5773139, 1.3712427);
//var loc2 = new GLatLng(52.4788314, 1.7577444);
//use this instead
var Location1 = new google.maps.LatLng(lat1, lng1);
var location2 = new google.maps.LatLng(lat2, lng2);
getDistance(loc1,loc2)
thanks
I would like to add another method here, you can also use Google Maps V3 inbuilt function 'google.maps.geometry.spherical.computeDistanceBetween
', by passing lat-long of both points as shown below
var latitude1 = 40.16;
var longitude1 = -0.66;
var latitude2 = 41.40;
var longitude2 = -3.18;
var distanceDifferenceInMeters = google.maps.geometry.spherical.computeDistanceBetween(
new google.maps.LatLng(latitude1, longitude1),
new google.maps.LatLng(latitude2, longitude2)
);
You will get distance results in meters.
Using Javascript calculate distance between 2 points in google maps
google.maps.LatLng.prototype.distanceFrom = function(latlng) {
var lat = [this.lat(), latlng.lat()]
var lng = [this.lng(), latlng.lng()]
var R = 6378137;
var dLat = (lat[1]-lat[0]) * Math.PI / 180;
var dLng = (lng[1]-lng[0]) * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat[0] * Math.PI / 180 ) * Math.cos(lat[1] * Math.PI / 180 ) *
Math.sin(dLng/2) * Math.sin(dLng/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return Math.round(d);
}
Example:
var loc1 = new GLatLng(52.5773139, 1.3712427);
var loc2 = new GLatLng(52.4788314, 1.7577444);
var dist = loc2.distanceFrom(loc1);
alert(dist/1000);
Value is returned in KM(Kilometer).
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly