calculate distance between two points google maps api code example
Example 1: calculate distance between two latitude longitude points in google maps api
var rad = function(x) {
return x * Math.PI / 180;
};
var getDistance = function(p1, p2) {
var R = 6378137;
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;
};
Example 2: calculate distance between two latitude longitude points in google maps api
google.maps.geometry.spherical.computeDistanceBetween (latLngA, latLngB);
Example 3: distance between two lat long points google maps api
google.maps.geometry.spherical.computeDistanceBetween (latLngA, latLngB);