我认为你不应该使用这个解决方案。几天前我随意想了一下,认为测量从特定点到网格方格的位置将基于圆而不是统一的网格。离0,0越远,精度就越低!
我的做法是在我的PostalCode类上有两个额外的值。每当我更新PostalCode上的Long/Lat时,我会计算距离Long 0,Lat 0的X,Y距离。
public static class MathExtender
{
public static double GetDistanceBetweenPoints(double sourceLatitude, double sourceLongitude, double destLatitude, double destLongitude)
{
double theta = sourceLongitude - destLongitude;
double distance =
Math.Sin(DegToRad(sourceLatitude))
* Math.Sin(DegToRad(destLatitude))
+ Math.Cos(DegToRad(sourceLatitude))
* Math.Cos(DegToRad(destLatitude))
* Math.Cos(DegToRad(theta));
distance = Math.Acos(distance);
distance = RadToDeg(distance);
distance = distance * 60 * 1.1515;
return (distance);
}
public static double DegToRad(double degrees)
{
return (degrees * Math.PI / 180.0);
}
public static double RadToDeg(double radians)
{
return (radians / Math.PI * 180.0);
}
}
然后我这样更新我的类:
private void CalculateGridReference()
{
GridReferenceX = MathExtender.GetDistanceBetweenPoints(0, 0, 0, Longitude);
GridReferenceY = MathExtender.GetDistanceBetweenPoints(0, 0, Latitude, 0);
}
现在,我在我的数据库中为每一行拥有一个距离(以英里为单位)的x,y网格距离,以从网格参考0,0开始计算。如果我想查找所有距某个经纬度5英里范围内的地点,我首先会得到X,Y网格参考点(比如25,75),然后在数据库中搜索20..30,70..80,并进一步使用内存过滤结果。
MathExtensder.GetDistanceBetweenPoints(candidate.Lat, candidate.Long, search.Lat, search.Long) < TheRadiusOfInterest
数据库部分非常快,而内存部分在较小的数据集上工作,使其更加准确。