我有一个包含位置的 mysql 表,例如:
ID latitude longitude value
1 11.11111 22.22222 1
2 33.33333 44.44444 2
3 11.11112 22.22223 5
我想选择彼此靠近的记录(在上面的示例中,第 1 行和第 3 行),以便我可以将它们作为一条记录插入到新表中。说近,就说100米。我希望新表是:
ID latitude longitude value
1 11.11111 22.22222 3
2 33.33333 44.44444 2
正如您所看到的,我想保留第一条记录的坐标,并在“值”列中插入两条记录的平均值。
我用于计算距离的公式如下:
R*SQRT(((lon2*c-lon1*c)*cos(0.5*(lat2*c+lat1*c)))^2 + (lat2*c-lat1*c)^2)
where
[lat1, lon1] the coordinates of the first location,
[lat2, lon2] the coordinates of the second location,
[R] the average radius of Earth in meters,
[c] a number to convert degrees to radians.
该公式适用于短距离。
所以,我的问题不是纬度、经度到距离的转换,而是我的 SQL。我知道如何选择与特定经纬度坐标最大距离为 100 米的记录,但我不知道如何选择彼此之间最大距离的记录。
我做到这一点的一种方法(并且有效)是在 PHP 中逐个循环记录,并为每一个记录进行 SQL 查询以选择附近的记录。但是这样,我在循环中执行 SQL 查询,据我所知,这是一个不好的做法,特别是如果记录有数千个。
我希望我说清楚了。如果没有,我很乐意为您提供更多信息。
谢谢你的帮助。