这是我基本上想要实现的图片:
所以正如标题所说,我想合并长/纬度点,它们的半径(例如 25 公里)在长/纬度点的边界框内触摸。
这是我非常简单的数据库结构:
+-------+-------------+------------+
| id | long | lat |
+-------+-------------+------------+
| 1 | -90.27137 | 50.00702 |
| 2 | -92.27137 | 52.00702 |
| 3 | -87.27137 | 48.00702 |
| 4 | -91.27137 | 51.00702 |
+-------+-------------+------------+
到目前为止,这是我的查询:
set @bottom_lat = -100.27137;
set @bottom_lon = 40.00702;
set @top_lat = -80.27137 ;
set @top_lon = 60.00702 ;
;
SELECT AVG(latitude), AVG(longitude)
FROM destination
WHERE latitude > @bottom_lat AND longitude > @bottom_lon AND latitude < @top_lat AND longitude < @top_lon
所以我的查询只是合并一个假想的边界框内的所有点,而不考虑半径。
我知道我可能不得不使用 Haversine 公式,但我在数学和 MySQL 方面很糟糕,这让事情变得有点困难。事实上,如果我只有一个半径,我最终可以合并点,但每个点都有自己的半径,我正在努力。
这是针对学生项目的,任何帮助都会非常感激。
参考:
-我对 SQL Fiddle 的查询:http ://sqlfiddle.com/#!2/3a42b/2 (在评论中包含一个用于 Haversine 公式的 SQL Fiddle 示例)
- MySQL 查询中的 Haversine 公式:(用于检查给定半径内的所有点)
SELECT*, ( 6371* acos( cos( radians(
@my_lat) ) * cos( radians(
destination.latitude ) ) * cos( radians(
destination.longitude ) - radians(
@my_lon) ) + sin( radians(
@my_lat) ) * sin( radians(
destination.latitude ) ) ) ) AS distance
FROM destination
ORDER BY distance limit 1
;