Geolocation zipcode距离范围公式。哪个公式是正确的?

Geolocation zipcode距离范围公式。哪个公式是正确的?,geolocation,geospatial,haversine,Geolocation,Geospatial,Haversine,我需要从一个zipcode中找到所有具有一定范围的zipcode。我有一个数据库中所有的zipcode lat/lon 我在网上发现了两个公式,它们之间略有不同。哪一个是正确的 公式1: def latRange = range/69.172 def lonRange = Math.abs(range/(Math.cos(Math.toRadians(zip.latitude)) * 69.172)); def minLat = zip.latitude - latRange def maxLa

我需要从一个zipcode中找到所有具有一定范围的zipcode。我有一个数据库中所有的zipcode lat/lon

我在网上发现了两个公式,它们之间略有不同。哪一个是正确的

公式1:

def latRange = range/69.172
def lonRange = Math.abs(range/(Math.cos(Math.toRadians(zip.latitude)) * 69.172));
def minLat = zip.latitude - latRange
def maxLat = zip.latitude + latRange
def minLon = zip.longitude - lonRange
def maxLon = zip.longitude + lonRange
公式2:(除以下内容外,与公式1相同:)

(第二个没有
Math.toRadians

在获得min/max Lat/Lon之后,我打算使用一个between条件搜索一个DB表哪个公式是正确的?

它取决于你的lat/long单位。如果以度为单位,则需要转换为弧度。

如果纬度和经度数据尚未以弧度为单位,则需要使用转换的数据。不过,您应该意识到,按照现在的设置方式,您最终将得到一个正方形范围

您最好在mysql查询中进行距离计算,使用毕达哥拉斯定理来计算距离,这样您就得到了一个循环范围。 这个问题应该可以帮助您开始以下内容:


如果您需要改进它的性能,可以使用索引对其进行索引。

我建议让db来完成所有繁重的工作。几个数据库都有地理附加组件,但这里有几个例子:,

我不认为数据是用弧度表示的。我在这里找到了:@Tihom-应该很容易分辨。如果你有超过π的任何值,它是以度为单位的。你应该在谷歌上搜索“Haversine forumula”。充其量,您上面的代码确定的是矩形,我认为您需要确定围绕中心点的圆内的zipcodes。
def lonRange = Math.abs(range/(Math.cos(zip.latitude) * 69.172));