这可能不清楚,也可能不清楚,如果我有误,请给我评论,或者您需要更多信息。 也许已经有一个解决方案可以满足我在 PHP 中的需求。
我正在寻找一个函数,可以从经度或纬度值中添加或减去距离。
原因:我有一个包含所有纬度和经度的数据库,并且想要形成一个查询来提取 X 公里(或英里)内的所有城市。 我的查询看起来像这样...
Select * From Cities Where (Longitude > X1 and Longitude < X2) And (Latitude > Y1 and Latitude < Y2)
Where X1 = Longitude - (distance)
Where X2 = Longitude + (distance)
Where Y1 = Latitude - (distance)
Where Y2 = Latitude + (distance)
我正在使用 PHP,使用 MySql 数据库。
也欢迎任何建议! :)
This may or may not be clear, leave me a comment if I am off base, or you need more information. Perhaps there is a solution out there already for what I want in PHP.
I am looking for a function that will add or subtract a distance from a longitude OR latitude value.
Reason: I have a database with all Latitudes and Longitudes in it and want to form a query to extract all cities within X kilometers (or miles). My query would look something like this...
Select * From Cities Where (Longitude > X1 and Longitude < X2) And (Latitude > Y1 and Latitude < Y2)
Where X1 = Longitude - (distance)
Where X2 = Longitude + (distance)
Where Y1 = Latitude - (distance)
Where Y2 = Latitude + (distance)
I am working in PHP, with a MySql Database.
Open to any suggestions also! :)
发布评论
评论(10)
我尝试使用上面的代码,当点之间的距离在 20-30 英里范围内时,答案偏差太大,而且我可以接受几英里的错误。 与我的一位绘图伙伴交谈后,我们想出了这个。 代码是Python的,但是你可以很容易地翻译它。 为了避免不断转换为弧度,我重新设计了数据库,将纬度/经度点从度数转换为弧度。 这样做的好处是,大部分数学运算只需完成一次。
当距离很小时,并且当距离增加到 200 英里时,在 10 英里左右的范围内,似乎非常准确(当然,到那时,您就会遇到“直线前进”与“铺好的道路”的问题)。
这是我上面提到的 ZipData 模型。
额外注意的是,您可以在 GeoNames.org 他们甚至还有一些您也可以使用的 Web 服务 API。
I Tried using the above code, and the answers were off by too much when the distance between points was in the 20-30 mile range, and I'm ok with a few miles of error. Talked with a mapping buddy of mine and we came up with this one instead. The code is python, but you can translate it pretty easily. In order to avoid the constant conversion to radians, I redid my database, converting the lat/lng points from degrees to Radians. The nice part about this is that the largest part of the math is mostly done once.
Seems to be very accurate when distance apart is small, and within 10 miles or so as the distance grows to 200 miles, (of course by then, you have issues with "as the crow flies" vs "paved roads").
Here is the model ZipData that I mention above.
An extra note, is that you can gets LOTS of geo data related to postal codes at GeoNames.org and they even have some webservice APIs you can use as well.
有很多(不好的选择)
使用数学公式(将 X1-X2 和 Y1-Y2 视为向量)来计算距离。
预先创建一个包含所有组合的查找表并保留距离。
考虑使用 MySQL 的 GIS 特定扩展。 这是我发现的关于此的一篇文章 。
There are many (bad options)
Calculate the distance using the mathematical formula (treat X1-X2 and Y1-Y2) as vectors.
Create a lookup table in advance with all the combinations and keep the distances.
Consider using a GIS-specific extension of MySQL. Here is one article I found about this.
lessthandot.com 实际上有 3 种不同的方法来做到这一点。 您必须稍微滚动一下博客,但它们就在那里。
http://blogs.lessthandot.com/
lessthandot.com actually has 3 different ways to do this. you'll have to scroll through the blogs a little but they're there.
http://blogs.lessthandot.com/
下面的函数来自 nerddinner 的(ASP.NET MVC 示例应用程序可在 codeplex 上使用)数据库 (MSSQL)。
我猜这可能会有帮助。
The function below is from the nerddinner's (ASP.NET MVC sample application available on codeplex) database (MSSQL).
I am guessing this could be helpful.
您可以使用毕达哥拉斯定理来计算两对纬度/经度点的接近度。
如果您有两个位置(Alpha 和 Beta),您可以使用以下方法计算它们之间的距离:
You can use Pythagoras' Theorem to calculate the proximity of two pairs of lat/lon points.
If you have two locations (Alpha and Beta) you can calculate their distance apart with:
使用以下 URL 中的设置,我构建了下面的查询。 (请注意我使用 codeIgnitor 查询数据库)
http://howto-use-mysql-spatial-ext.blogspot.com/2007/11/using-circular-area-selection.html
Using the setup from the following URL, Ive built the query below. (Please note Im using codeIgnitor to query the database)
http://howto-use-mysql-spatial-ext.blogspot.com/2007/11/using-circular-area-selection.html
不要重新发明轮子。 这是一个空间查询。 使用 MySQL 的内置空间扩展,用于将经纬度坐标数据存储在 本机 MySQL 几何列类型。 然后使用 Distance 函数用于查询彼此之间指定距离内的点。
免责声明:这是基于阅读文档,我自己还没有尝试过。
Don't reinvent the wheel. This is a spatial query. Use MySQL's built-in spatial extensions to store the latitude-longitude coordinate data in the native MySQL geometry column type. Then use the Distance function to query for points that are within a specified distance of one another.
Disclaimer: this is based on reading the documentation, I haven't tried this myself.
这是一个 MySQL 查询,它将完全满足您的需求。 请记住,这样的事情通常是近似值,因为地球不是完美的球形,也没有考虑到山脉、丘陵、山谷等。我们在 AcademicHomes.com 使用 PHP 和 MySQL,它返回 $latitude、$longitude 的 $radius 英里内的记录。
This is a MySQL query that will do exactly what you want. Keep in mind things like this are approximations generally, as the earth is not perfectly spherical nor does this take into account mountains, hills, valleys, etc.. We use this code on AcademicHomes.com with PHP and MySQL, it returns records within $radius miles of $latitude, $longitude.
编辑:如果您在某个地方有世界上所有城市及其纬度的列表。 又长。 值,你可以进行查找。 在这种情况下,请参阅下面的第一个链接,了解计算纬度上一个纵向度数宽度的公式 :
老实说,这个问题背后的复杂性是这样的您最好使用 Google 地图等服务来获取数据。 具体来说,地球不是一个完美的球体,两度之间的距离随着您靠近/远离赤道而变化。
有关我的意思的示例,请参阅 http://en.wikipedia.org/wiki/Geographic_coefficient_system ,然后查看Google Maps API。
EDIT: If you have, somewhere, a list of all of the cities in the world along with their lat. and long. values, you can do a lookup. In this case, see my first link below for the formula to calculate the width of one longitudinal degree at latitude :
Honestly, the complications behind this problem are such that you'd be far better off using a service such as Google Maps to get your data. Specifically, the Earth is not a perfect sphere, and the distance between two degrees varies as you are closer to / further from the equator.
See http://en.wikipedia.org/wiki/Geographic_coordinate_system for examples of what I mean, and check out the Google Maps API.
根据您包含的城市数量,您可以预先计算该列表。 我们在这里针对内部应用程序执行此操作,其中 +100m 的误差对于我们的设置来说太大了。 它的工作原理是有一个两个键表:位置 1、位置 2、距离。 然后我们可以非常快速地从位置 1 拉回位置 x 距离。
而且由于计算可以离线完成,因此不会影响系统的运行。 用户还可以更快地获得结果。
Depending on how many cities you are including, you can precompute the list. We do this here for an internal application where an inaccuracy of +100m is too much for our setup. It works by having a two key table of location1, location2, distance. We can then pull back locations x distance from location1 very quickly.
Also since the calcs can be done offline, it doesn't impact the running of the system. Users also get faster results.