在距纬度/经度一定距离内查询时出现奇怪的结果
我在这里获得了查询帮助,以查找某个点(经纬度)特定半径内的记录。该查询非常适合查找距离实际中心不太近的记录。所以它可以找到 400 英里以内的东西,但不能找到 20 英里内的东西
。我正在使用 MySQL。这是查询:
select id , 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_table HAVING distance < 1000 ORDER BY distance LIMIT 0 , 50
这是一个小数据集:
lat lng
| 0.000000 | 0.000000 |
| 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 |
我的中心起点是旧金山,纬度/经度为 37.787144 , -122.493263
因此找不到附近的纬度/经度。
谢谢你, 亚历克斯
I got help here with a query to find records withing a certain radius of a point (lat/lng). The query works great for finding records that are not too close to the actual center. So it can find things within 400 miles, but not 20.
I am using MySQL. Here is the query:
select id , 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_table HAVING distance < 1000 ORDER BY distance LIMIT 0 , 50
And here is a small data set:
lat lng
| 0.000000 | 0.000000 |
| 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 |
And my center starting point is San Francisco, which is lat/lng of 37.787144 , -122.493263
And so none of the nearby lat/lng can be found.
Thank you,
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 select 语句中有一些硬编码值(37、-122.517349、37.780182)与您的中心点不对应。
You have some hardcoded values in your select statement (37, -122.517349, 37.780182) that don't correspond to your center point.
保持纬度是 37 还是 37.780182 的一致性可能会有所帮助。
acos(cos(LAT0) * cos(LAT1) * cos(LNG1 - LNG0) + sin(LAT0) * sin(LAT1))) 背后的理论基础是什么?如果您将地球建模为球体,那么您需要 acos(3D 嵌入的点积),即 acos(cos(LAT0) * cos(LAT1) + sin(LAT0) * sin(LAT1) * (cos(LNG0) ) * cos(LNG1) + sin(LNG0) * sin(LNG1)))。It would probably help to be consistent on whether the latitude is 37 or 37.780182.
What's the theoretical basis behind acos(cos(LAT0) * cos(LAT1) * cos(LNG1 - LNG0) + sin(LAT0) * sin(LAT1)))? If you're modelling the globe as a sphere then you want acos(dot product of the 3D embedding) which is acos(cos(LAT0) * cos(LAT1) + sin(LAT0) * sin(LAT1) * (cos(LNG0) * cos(LNG1) + sin(LNG0) * sin(LNG1))).使用此方法后,我制作了一个教程和一个将地址转换为纬度/经度的工具。也许会有帮助:http://www.comehike.com/utils/address_to_geolocation.php
after working with this, I made a tutorial and a tool that converts address to lat/lng. Maybe it will be helpful: http://www.comehike.com/utils/address_to_geolocation.php