查找纬度/经度边界框中的位置
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在比较纬度和纬度将数据库中的经度更改为提交值的纬度。
另外,查询中不需要任何括号,它只会使查询变得混乱且更加复杂。
你想要类似的东西:
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:
如果您的应用程序可以有
$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 aNOT BETWEEN $northeast_lng AND $southwest_lng
in that case.效果会
更好吗?
would
work better?