【问题标题】:Get places near geographical coordinates获取地理坐标附近的地点
【发布时间】:2014-08-17 10:46:40
【问题描述】:

我有一个包含地理坐标 (DB 2) 的数据库,例如 45.062792、8.892737 和另一个具有其他坐标 (DB 1) 的数据库。

我想使用 DB 1 的一个坐标,然后我想以用户给定的半径(如 10 公里、5 公里、500 公吨)计算 DB 2 中先前给出的半径内的所有行。

我想在没有(例如)谷歌地图或其他在线地图的情况下计算所有结果,因为使用有限制,我希望能够通过我的服务器上的 php 代码管理结果。

【问题讨论】:

    标签: php mysql geolocation


    【解决方案1】:

    使用使用纬度/经度坐标和半径作为输入的 Haversine 公式。 (我假设您将坐标存储为单独的 lat 和 lon 列)。

    您的查询大致如下所示:

    SELECT *, 3956 * 2 * ASIN(SQRT(POWER(SIN((@orig_lat - abs(tablename.lat)) * pi()/180 / 2),2) + COS(@orig_lat * pi()/180 ) * COS( 
    abs
    (tablename.lat) *  pi()/180) * POWER(SIN((@orig_lon – tablename.lon) *  pi()/180 / 2), 2) ))
    as distance
    FROM tablename
    HAVING distance < @radius
    ORDER BY distance 
    LIMIT 10;
    

    将@orig_lat 替换为给定的纬度值,将@orig_lon 替换为给定的经度值,将@radius 替换为以公里为单位的值。

    这将根据您在给定半径内来自 DB1 的坐标返回来自 DB2 的所有坐标。

    更多信息在这里:http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

    【讨论】:

    • 感谢您的建议顺便说一句,在阅读了您链接的文档后,我不知道最好使用您发布的代码还是这个 SELECT destination.*, 3956 * 2 * ASIN(SQRT( POWER(SIN((orig.lat - dest.lat) * pi()/180 / 2), 2) +COS(orig.lat * pi()/180) * COS(dest.lat * pi()/180) * POWER(SIN((orig.lon -dest.lon) * pi()/180 / 2), 2) )) as distance FROM users destination, users origin WHERE origin.id=userid and destination.longitude between lon1 and lon2 and destination.latitude between lat1 and lat2 标记为的代码更快...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2015-05-16
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多