查找纬度/经度边界框中的位置

发布于 2024-10-26 07:37:23 字数 442 浏览 0 评论 0原文

我有 4 个变量:

$southwest_lat、$southwest_lng、$northeast_lat、$northeast_lng

我还有一个数据库表,其中包含字段:名称、纬度和经度。

// First attempt, likely terribly wrong
$sql = "SELECT * FROM items WHERE
(Latitude >= '$southwest_lat' AND Latitude <= '$northeast_lat') AND
(Longitude >= '$southwest_lng' AND Longitude <= '$northeast_lng')";

如何在坐标形成的边界框中找到数据库项?

编辑:谢谢大家,更正了查询的经度部分。

I have 4 variables:

$southwest_lat, $southwest_lng, $northeast_lat, $northeast_lng

I also have a database table, with fields: Name, Latitude, and Longitude.

// First attempt, likely terribly wrong
$sql = "SELECT * FROM items WHERE
(Latitude >= '$southwest_lat' AND Latitude <= '$northeast_lat') AND
(Longitude >= '$southwest_lng' AND Longitude <= '$northeast_lng')";

How would I find DB items within the boundary box formed by the coordinates?

EDIT: Thanks guys, corrected the Longitude part of the query.

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

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

发布评论

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

评论(3

挽手叙旧 2024-11-02 07:37:23

您正在比较纬度和纬度将数据库中的经度更改为提交值的纬度。

另外,查询中不需要任何括号,它只会使查询变得混乱且更加复杂。

你想要类似的东西:

SELECT 
    *
FROM 
    items
WHERE
    Latitude >= $southwest_lat AND
    Latitude <= $northeast_lat AND
    Longitude >= $southwest_long AND
    Longitude <= $northeast_long;

You are comparing the latitude & longitude in the database to JUST the latitudes of the submitted values.

Also, you don't need any parentheses in the query, it just makes it cluttered and more complicated.

You want something like:

SELECT 
    *
FROM 
    items
WHERE
    Latitude >= $southwest_lat AND
    Latitude <= $northeast_lat AND
    Longitude >= $southwest_long AND
    Longitude <= $northeast_long;
风尘浪孓 2024-11-02 07:37:23

如果您的应用程序可以有 $southwest_lng > $northeast_lng(180 度环绕,例如使用 Google 地图时),在这种情况下,您可能需要检查并使用NOT BETWEEN $northeast_lng AND $southwest_lng

In case your application can have $southwest_lng > $northeast_lng (180 degrees wrap-around, e.g. when using Google Maps), you may want to check that and use a NOT BETWEEN $northeast_lng AND $southwest_lng in that case.

花心好男孩 2024-11-02 07:37:23

效果会

SELECT * FROM items 
WHERE Latitude BETWEEN $southwest_lat AND $northeast_lat 
AND Longitude BETWEEN $southwest_lng AND $northeast_lng

更好吗?

would

SELECT * FROM items 
WHERE Latitude BETWEEN $southwest_lat AND $northeast_lat 
AND Longitude BETWEEN $southwest_lng AND $northeast_lng

work better?

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