PHP/MySQL:从数据库中选择靠近给定位置的位置
在 PHP 中,我有以下代码用于计算两个位置之间的距离:
<?php
function distance($lat1, $long1, $lat2, $long2) {
// DEGREE TO RADIAN
$latitude1 = $lat1/180*pi();
$longitude1 = $long1/180*pi();
$latitude2 = $lat2/180*pi();
$longitude2 = $long2/180*pi();
// FORMULA: e = ARCCOS ( SIN(Latitude1) * SIN(Latitude2) + COS(Latitude1) * COS(Latitude2) * COS(Longitude2-Longitude1) ) * EARTH_RADIUS
$distance = acos(sin($latitude1)*sin($latitude2)+cos($latitude1)*cos($latitude2)*cos($longitude2-$longitude1))*6371;
return $distance;
}
echo distance(9.9921962, 53.5534074, 9.1807688, 48.7771056); // Hamburg, DE - Stuttgart, DE
?>
但现在,我想通过 PHP 从我的 MySQL 数据库中选择靠近给定位置的位置:
- 用户输入他的家乡
- 我的脚本通过以下方式获取纬度/经度值: Google API
- 在我的数据库中,我有大约 200 个位置,其中有一个纬度值字段和一个经度值字段
- 我需要 PHP 和 MySQL 的代码来选择距离用户家乡最近的 10 个位置
我希望你能帮我。 提前致谢!
In PHP, I have the following code for calculating the distance between two locations:
<?php
function distance($lat1, $long1, $lat2, $long2) {
// DEGREE TO RADIAN
$latitude1 = $lat1/180*pi();
$longitude1 = $long1/180*pi();
$latitude2 = $lat2/180*pi();
$longitude2 = $long2/180*pi();
// FORMULA: e = ARCCOS ( SIN(Latitude1) * SIN(Latitude2) + COS(Latitude1) * COS(Latitude2) * COS(Longitude2-Longitude1) ) * EARTH_RADIUS
$distance = acos(sin($latitude1)*sin($latitude2)+cos($latitude1)*cos($latitude2)*cos($longitude2-$longitude1))*6371;
return $distance;
}
echo distance(9.9921962, 53.5534074, 9.1807688, 48.7771056); // Hamburg, DE - Stuttgart, DE
?>
But now, I want to select locations close to a given location via PHP from my MySQL database:
- The user enters his hometown
- My script gets the latitude/longitude values via the Google API
- In my database, I have about 200 locations with a field for the latitude value and a field for the longitude value
- I need a code for PHP and MySQL to select the 10 locations which are closest to the user's hometown
I hope you can help me. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
MySQL 大圆距离(半正矢公式) 正是您所需要的。
然而,只有 200 条记录,您也可以加载所有记录并使用代码检查它们。 数据集确实太小,无法过多担心数据库与代码或任何其他此类优化。
在 PHP 中计算邮政编码之间的距离 有几个 PHP 实现:这个算法。
地理位置邻近搜索与您遇到的问题几乎完全相同。
MySQL Great Circle Distance (Haversine formula) does exactly what you need.
With only 200 records however you may as well just load them all and check them with code. The data set is really way too small to be worrying too much about database vs code or any other such optimizations.
Calculating distance between zip codes in PHP has a couple of PHP implementations of this algorithm.
Geo Proximity Search is pretty much the exact same problem you have.
也许类似
or
(直接从 PHP 代码翻译)
:1
和:2
是$lat2/180*pi()
和$long2/180*pi() 分别。
Maybe something like
or
(directly translated from the PHP code)
:1
and:2
is$lat2/180*pi()
and$long2/180*pi()
respectively.这就是半正矢公式。 您可以将 PHP 直接转换为 SQL,以便可以在空间上查询数据库(另一种方法是从数据库中提取每条记录并通过 PHP 运行数据)。 MySQL 提供了您需要的所有数学函数。
我为一个提供基于邮政/邮政编码的距离查找的商业网站执行此操作,因此在没有特定 GIS 功能的情况下当然可以实现。
That's the Haversine formula. You can translate the PHP directly into SQL so you can query the database spatially (the alternative being to pull every record out of the DB and run the data through PHP). MySQL provides all the maths functions that you need.
I did this for a commercial website which provided post/zipcode based distance lookups, so its certainly possible without specific GIS functions.
MySQL 中有一些函数可以用来完成此任务。
http://dev.mysql.com/doc/refman/ 5.0/en/gis-introduction.html
There are MySQL functions available for doing exactly this.
http://dev.mysql.com/doc/refman/5.0/en/gis-introduction.html
MySQL 能够对行进行地理空间索引。 你可能不需要自己做这个数学运算(你可以让 MySQL 计算两个地理对象之间的距离并按其排序......)。
请参阅:http://forums.mysql.com/read.php?23, 159205,159205
MySQL has the ability to index rows geospatially. You might not need to do this math by yourself (you can just ask MySQL to compute the distance between two geo objects and sort by that..).
See: http://forums.mysql.com/read.php?23,159205,159205
刚刚跨过这个话题。 也许有人会对这个函数感兴趣,在MySql 5.7中测试过:
调用:
Just came a cross this topic. Maybe someone will be interested in this function, tested in MySql 5.7:
Calling:
为什么不使用 MySQL 的地理空间功能...? 不仅仅在开玩笑。
如果 200 条记录是城镇等实际地点,那么您可以使用 GeoNames 的 API 作为替代方案吗?
以下网络服务将提供与所提供的纬度和经度最接近的 10 个位置:
http ://ws.geonames.org/findNearby?lat=47.3&lng=9
来源:http://www.geonames.org/export/web-services.html#findNearbyPlaceName
完整列表:http://www.geonames.org/export/ws-overview.html
Why don't you use MySQL's geospatial features...? No, just kidding.
If the 200 records are actual places like towns, etc then as an alternative could you use GeoNames' API?
The following webservice will provide the 10 closest locations to the lat and lng provided:
http://ws.geonames.org/findNearby?lat=47.3&lng=9
Source: http://www.geonames.org/export/web-services.html#findNearbyPlaceName
Full list: http://www.geonames.org/export/ws-overview.html