Home >Web Front-end >JS Tutorial >How to Calculate the Distance Between Two Points in Google Maps V3?
Determining Distance between Points in Google Maps V3
Calculating the distance between two markers in Google Maps V3 requires an updated approach compared to the 'distanceFrom' function employed in V2.
Custom Calculation Using Haversine Formula
If custom calculation is preferred, the Haversine formula can be utilized:
var rad = function(x) { return x * Math.PI / 180; }; var getDistance = function(p1, p2) { var R = 6378137; // Earth's mean radius in meters 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; // returns distance in meters };
The above is the detailed content of How to Calculate the Distance Between Two Points in Google Maps V3?. For more information, please follow other related articles on the PHP Chinese website!