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

发布于 2024-10-11 19:47:10 字数 1712 浏览 1 评论 0原文

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

我之前发布过这个帖子,社区帮助我找到了我需要的公式:在 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 纬度/经度,这是我表示该记录没有纬度/经度的方式。

公式应该是这样的:

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 英里。你知道为什么吗?

I have a query that attempts to find things within a certain geolocation, but the results that it brings back are a little bit... strange.

I had previously posted this thread and the community helped me find a formula that I needed: Querying within longitude and latitude in MySQL but the query now gives really wacky results.

My data is surrounding the city of San Francisco which has the lat/lng 37.780182 , -122.517349

Query:

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

So as you see - even if I make radius 10000 it still doesn't find much, so I wonder if my query is off. Some of the results it brings back even have 0,0 for lat/lng which is my way to denote that there are no lat/lng for that record.

Here is how the formula is supposed to look like:

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;

Thank you and sorry for the painfully long question ! By the way, I am using MySQL.


Hmmm,

I just tried with the exact query you gave and it worked with 50% accuracy. Here is a sample of the data set:

    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 |

So now the query returned all the items that were many miles away, but not the items which were under 100 miles away. Like this item ( 37.787144 , -122.493263 ) was never getting returned even though it is just about 50 miles from the originating point. Would you happen to know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-10-18 19:47:10

我认为您颠倒了中心点的纬度(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

I think you reversed the latitude (37.780182) and longitude (-122.517349) of your central point, try:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文