I want to work out the distance between 2 latlon points. The simple distance formula http://www.purplemath.com/modules/distform.htm is not correct because we are dealing with 2 different measures (lat开发者_开发技巧 and lon).
Is there a standard solution to this problem?
use Haversine formula
.
see this link http://www.movable-type.co.uk/scripts/latlong.html
try this, This uses the ‘haversine’ formula to calculate great-circle distances between the two points – that is, the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the points (ignoring any hills!).
Haversine formula:
R = earth’s radius (mean radius = 6,371km)
Δlat = lat2− lat1 Δlong = long2− long1 a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2) c = 2.atan2(√a, √(1−a)) d = R.c
or go with the link,http://www.movable-type.co.uk/scripts/latlong.html
Try this javascript haversine function alongside the torad() helper function, which I use for my map app
function calculateHaversineDistance(lat1x, lon1, lat2x, lon2) {
var R = 6371; // km
var dLat = toRad(lat2x-lat1x);
var dLon = toRad(lon2-lon1);
var lat1 = toRad(lat1x);
var lat2 = toRad(lat2x);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}
function toRad(x) {
return x * Math.PI / 180;
}
Hope this helps.
精彩评论