gpt4 book ai didi

mysql - 查询地理位置附近的事物?

转载 作者:可可西里 更新时间:2023-11-01 06:40:55 25 4
gpt4 key购买 nike

我有一个查询试图在特定地理位置内查找内容,但返回的结果有点……奇怪。

我之前发布过这个帖子,社区帮助我找到了我需要的公式:Querying within longitude and latitude in MySQL但是查询现在给出了非常古怪的结果。

我的数据围绕旧金山市,经纬度 37.780182 , -122.517349

查询:

SELECT
id,
hike_title,
lat,
lng,
( 3959 * acos( cos( radians(37) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(37.780182) )
+ sin( radians(-122.517349) ) * sin( radians( lat ) )
)
) AS distance
FROM
my_awesome_table
HAVING
distance < 10000
ORDER BY
distance
LIMIT
0 , 50

如您所见 - 即使我将半径设置为 10000,它仍然找不到太多,所以我想知道我的查询是否已关闭。它带回的一些结果甚至有 0,0 的 lat/lng,这是我表示该记录没有 lat/lng 的方式。

公式应该是这样的:

SELECT
id,
( 3959 * acos( cos( radians(37) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(-122) )
+ sin( radians(37) ) * sin( radians( lat ) )
)
) AS distance
FROM
markers
HAVING
distance < 25
ORDER BY
distance
LIMIT
0 , 20;

谢谢你,很抱歉提出这么长的问题!顺便说一下,我正在使用 MySQL。


嗯,

我刚刚尝试了您提供的确切查询,它的准确率为 50%。这是数据集的示例:

    lat          lng    
| 37.223465 | -122.090363 |
| 39.320980 | -111.093735 |
| 38.031715 | -84.495132 |
| 37.787144 | -122.493263 |
| 52.975361 | -1.458620 |
| 40.848557 | -111.906883 |
| 40.572498 | -111.859718 |

现在查询返回了很多英里之外的所有项目,但不返回 100 英里以外的项目。就像这个项目( 37.787144 , -122.493263 )永远不会被退回,即使它离起点只有大约 50 英里。你知道为什么吗?

最佳答案

我认为你颠倒了中心点的纬度 (37.780182) 和经度 (-122.517349),试试:

select
id,
hike_title,
lat,
lng,
( 3959 * acos( cos( radians(37) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(-122.517349) )
+ sin( radians(37.780182) ) * sin( radians( lat ) )
)
) AS distance
FROM
my_awesome_table
HAVING
distance < 10000
ORDER BY
distance
LIMIT
0 , 50

关于mysql - 查询地理位置附近的事物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4697624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com