寻找'X'公里(或英里)内的城市

Mic*_*ICE 12 mysql gis distance coordinates coordinate-systems

这可能会也可能不会很清楚,如果我不在基础上,或者您需要更多信息,请给我留言.也许有一个解决方案已经在我想要的PHP中.

我正在寻找一个函数,它将增加或减去经度OR纬度值的距离.

原因:我有一个包含所有纬度和经度的数据库,并希望形成一个查询来提取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)
Run Code Online (Sandbox Code Playgroud)

我在PHP中工作,使用MySql数据库.

也欢迎任何建议!:)

Kei*_*Jr. 17

这是一个MySQL查询,它将完全符合您的要求.请记住这样的事情一般是近似值,因为地球不是完美的球形,也不考虑山脉,山丘,山谷等.我们在AcademicHomes.com上使用PHP和MySQL的代码,它返回$内的记录半径英里的$ latitude,$经度.

$res = mysql_query("SELECT
    * 
FROM
    your_table
WHERE
    (
        (69.1 * (latitude - " . $latitude . ")) * 
        (69.1 * (latitude - " . $latitude . "))
    ) + ( 
        (69.1 * (longitude - " . $longitude . ") * COS(" . $latitude . " / 57.3)) * 
        (69.1 * (longitude - " . $longitude . ") * COS(" . $latitude . " / 57.3))
    ) < " . pow($radius, 2) . " 
ORDER BY 
    (
        (69.1 * (latitude - " . $latitude . ")) * 
        (69.1 * (latitude - " . $latitude . "))
    ) + ( 
        (69.1 * (longitude - " . $longitude . ") * COS(" . $latitude . " / 57.3)) * 
        (69.1 * (longitude - " . $longitude . ") * COS(" . $latitude . " / 57.3))
    ) ASC");
Run Code Online (Sandbox Code Playgroud)

  • 嘿Mike,如果性能很重要,我在'纬度'上建立一个INDEX,在'经度'上设置一个INDEX,我通过使用BETWEEN子句将查询限制为较小的记录子集来调整上述查询.添加以下内容:WHERE纬度BETWEEN $ latitude - ($ radius/70)AND $ latitude +($ radius/70)AND经度BETWEEN $经度 - ($ radius/70)AND $ longitude +($ radius/70). ..这允许数据库使用纬度和经度上的索引.常数70是因为经过的纬度或经度的最大距离是大约70英里. (3认同)