Lio*_*gan 32
Haversine公式假设球形地球.然而,耳朵的形状更复杂.扁平球体模型将提供更好的结果.
如果需要这样的准确度,你应该更好地使用Vincenty逆公式.有关详细信息,请参见http://en.wikipedia.org/wiki/Vincenty's_formulae.使用它,您可以获得球体模型的0.5mm精度.
没有完美的公式,因为地球的真实形状太复杂而无法用公式表达.此外,地球的形状因气候事件而变化(见http://www.nasa.gov/centers/goddard/earthandsun/earthshape.html),并且由于地球自转而随时间变化.
您还应注意,上述方法不考虑海拔高度,并假设海平面扁球体.
编辑2010年7月10日:我发现少数情况下Vincenty逆公式不会收敛到声明的准确度.更好的想法是使用GeographicLib(参见http://sourceforge.net/projects/geographiclib/),这也更准确.
这是一个:http://www.movable-type.co.uk/scripts/latlong.html
使用Haversine公式:
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
Run Code Online (Sandbox Code Playgroud)
小智 5
应用Haversine公式来查找距离.请参阅下面的C#代码以查找2个坐标之间的距离.更好的是,如果您想要查找特定半径内的商店列表,您可以WHERE在SQL中应用子句或在C#中应用LINQ过滤器.
这里的公式是以公里为单位,你必须改变相关的数字,它将工作数英里.
例如:将6371.392896转换为里程.
DECLARE @radiusInKm AS FLOAT
DECLARE @lat2Compare AS FLOAT
DECLARE @long2Compare AS FLOAT
SET @radiusInKm = 5.000
SET @lat2Compare = insert_your_lat_to_compare_here
SET @long2Compare = insert_you_long_to_compare_here
SELECT * FROM insert_your_table_here WITH(NOLOCK)
WHERE (6371.392896*2*ATN2(SQRT((sin((radians(GeoLatitude - @lat2Compare)) / 2) * sin((radians(GeoLatitude - @lat2Compare)) / 2)) + (cos(radians(GeoLatitude)) * cos(radians(@lat2Compare)) * sin(radians(GeoLongitude - @long2Compare)/2) * sin(radians(GeoLongitude - @long2Compare)/2)))
, SQRT(1-((sin((radians(GeoLatitude - @lat2Compare)) / 2) * sin((radians(GeoLatitude - @lat2Compare)) / 2)) + (cos(radians(GeoLatitude)) * cos(radians(@lat2Compare)) * sin(radians(GeoLongitude - @long2Compare)/2) * sin(radians(GeoLongitude - @long2Compare)/2)))
))) <= @radiusInKm
Run Code Online (Sandbox Code Playgroud)
如果您想在C#中执行Haversine公式,
double resultDistance = 0.0;
double avgRadiusOfEarth = 6371.392896; //Radius of the earth differ, I'm taking the average.
//Haversine formula
//distance = R * 2 * aTan2 ( square root of A, square root of 1 - A )
// where A = sinus squared (difference in latitude / 2) + (cosine of latitude 1 * cosine of latitude 2 * sinus squared (difference in longitude / 2))
// and R = the circumference of the earth
double differenceInLat = DegreeToRadian(currentLatitude - latitudeToCompare);
double differenceInLong = DegreeToRadian(currentLongitude - longtitudeToCompare);
double aInnerFormula = Math.Cos(DegreeToRadian(currentLatitude)) * Math.Cos(DegreeToRadian(latitudeToCompare)) * Math.Sin(differenceInLong / 2) * Math.Sin(differenceInLong / 2);
double aFormula = (Math.Sin((differenceInLat) / 2) * Math.Sin((differenceInLat) / 2)) + (aInnerFormula);
resultDistance = avgRadiusOfEarth * 2 * Math.Atan2(Math.Sqrt(aFormula), Math.Sqrt(1 - aFormula));
Run Code Online (Sandbox Code Playgroud)
DegreesToRadian是我自定义创建的一个函数,它是一个简单的1个衬里"Math.PI * angle / 180.0