function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {var R = 6371; // Radius of the earth in kmvar dLat = deg2rad(lat2-lat1); // deg2rad belowvar dLon = deg2rad(lon2-lon1);var a =Math.sin(dLat/2) * Math.sin(dLat/2) +Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *Math.sin(dLon/2) * Math.sin(dLon/2);var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));var d = R * c; // Distance in kmreturn d;}
function deg2rad(deg) {return deg * (Math.PI/180)}
//put it on the map if within the range of a specified radi assuming 100,000,000 unitsvar iconpoint = Map.getPoint(pp.latitude, pp.longitude);var centerpoint = Map.getPoint(Settings.CenterLatitude, Settings.CenterLongitude);
//approx ~200 units to show only half of the globe from the default center radiusif (isMapPlacemarkInRadius(centerpoint, iconpoint, 120)) {addPlacemark(pp.latitude, pp.longitude, pp.name);}else {otherSidePlacemarks.push({latitude: pp.latitude,longitude: pp.longitude,name: pp.name});
}
Public Enum DistanceTypeMilesKiloMetersEnd Enum
Public Structure PositionPublic Latitude As DoublePublic Longitude As DoubleEnd Structure
Public Class Haversine
Public Function Distance(Pos1 As Position,Pos2 As Position,DistType As DistanceType) As Double
Dim R As Double = If((DistType = DistanceType.Miles), 3960, 6371)
Dim dLat As Double = Me.toRadian(Pos2.Latitude - Pos1.Latitude)
Dim dLon As Double = Me.toRadian(Pos2.Longitude - Pos1.Longitude)
Dim a As Double = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(Me.toRadian(Pos1.Latitude)) * Math.Cos(Me.toRadian(Pos2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2)
Dim c As Double = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)))
Dim result As Double = R * c
Return result
End Function
Private Function toRadian(val As Double) As Double
Return (Math.PI / 180) * val
End Function
End Class
HalfPi = 1.5707963;R = 3956; /* the radius gives you the measurement unit*/
a = HalfPi - latoriginrad;b = HalfPi - latdestrad;u = a * a + b * b;v = - 2 * a * b * cos(longdestrad - longoriginrad);c = sqrt(abs(u + v));return R * c;
function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2,units) {var R = 6371; // Radius of the earth in kmvar dLat = deg2rad(lat2-lat1); // deg2rad belowvar dLon = deg2rad(lon2-lon1);var a =Math.sin(dLat/2) * Math.sin(dLat/2) +Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *Math.sin(dLon/2) * Math.sin(dLon/2);var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));var d = R * c;var miles = d / 1.609344;
if ( units == 'km' ) {return d;} else {return miles;}}
CREATE FUNCTION `distance`(a POINT, b POINT)RETURNS doubleDETERMINISTICBEGIN
RETURN
GLength( LineString(( PointFromWKB(a)), (PointFromWKB(b)))) * 100000; -- To Make the distance in meters
END;
a= 6378.137#equitorial radius in kmb= 6356.752#polar radius in km
def Distance(lat1, lons1, lat2, lons2):lat1=math.radians(lat1)lons1=math.radians(lons1)R1=(((((a**2)*math.cos(lat1))**2)+(((b**2)*math.sin(lat1))**2))/((a*math.cos(lat1))**2+(b*math.sin(lat1))**2))**0.5 #radius of earth at lat1x1=R1*math.cos(lat1)*math.cos(lons1)y1=R1*math.cos(lat1)*math.sin(lons1)z1=R1*math.sin(lat1)
lat2=math.radians(lat2)lons2=math.radians(lons2)R2=(((((a**2)*math.cos(lat2))**2)+(((b**2)*math.sin(lat2))**2))/((a*math.cos(lat2))**2+(b*math.sin(lat2))**2))**0.5 #radius of earth at lat2x2=R2*math.cos(lat2)*math.cos(lons2)y2=R2*math.cos(lat2)*math.sin(lons2)z2=R2*math.sin(lat2)
return ((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)**0.5
SELECT UserId, ( 3959 * acos( cos( radians( your latitude here ) ) * cos( radians(latitude) ) *cos( radians(longitude) - radians( your longitude here ) ) + sin( radians( your latitude here ) ) *sin( radians(latitude) ) ) ) AS distance FROM user HAVINGdistance < 5 ORDER BY distance LIMIT 0 , 5;
dlon = lon2 - lon1dlat = lat2 - lat1a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2c = 2 * atan2( sqrt(a), sqrt(1-a) )distance = R * c (where R is the radius of the Earth)
R = 6367 km OR 3956 mi
lat1, lon1: The Latitude and Longitude of point 1 (in decimal degrees)lat2, lon2: The Latitude and Longitude of point 2 (in decimal degrees)unit: The unit of measurement in which to calculate the results where:'M' is statute miles (default)'K' is kilometers'N' is nautical miles
Direct method for 100,000 points94.9 ms ± 25 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)9.79 s ± 1.4 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
Inverse method for 100,000 points1.5 s ± 504 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)24.2 s ± 3.91 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)c = 2 ⋅ atan2( √a, √(1−a) )d = R ⋅ c
下面给出了一个计算两点之间距离的例子
假设我要计算从新德里到伦敦的距离,那么我该如何使用这个公式:
New delhi co-ordinates= 28.7041° N, 77.1025° ELondon co-ordinates= 51.5074° N, 0.1278° W
var R = 6371e3; // metresvar φ1 = 28.7041.toRadians();var φ2 = 51.5074.toRadians();var Δφ = (51.5074-28.7041).toRadians();var Δλ = (0.1278-77.1025).toRadians();
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +Math.cos(φ1) * Math.cos(φ2) *Math.sin(Δλ/2) * Math.sin(Δλ/2);var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // metresd = d/1000; // km