【问题标题】:Calculating Distance using lat,long between other cities and user selected city使用 lat,long 计算其他城市和用户选择的城市之间的距离
【发布时间】:2018-03-06 17:11:16
【问题描述】:

我希望根据用户搜索计算两个纬度、经度之间的距离。例如,如果用户在下表中搜索“雅典”,那么我必须计算雅典与列表中其他城市的距离。

State   City         lat    long
NY  Holtsville  40.813078   -73.046388
NY  Brunswick   40.813223   -73.049288
MA  Agawam      42.071523   -72.624257
FL  Minneola    28.577556   -86.818296
GA  Athens      33.903503   -83.318464
AL  Graysville  33.621087   -86.958247

我知道我们可以使用 st_distance_sphere(POINT({long1}, {lat1}), POINT({long2}, {lat2}))

所以基本上我正在寻找一个选择查询来获取雅典市的经纬度,然后使用 st_distance_sphere 公式我需要计算其他城市的距离

【问题讨论】:

    标签: mysql spatial


    【解决方案1】:

    您必须在 SQL 查询中应用距离公式来查找两个给定坐标之间的距离。

    select
        2 * 3961 * asin(sqrt((sin(radians((lat2 - lat1) / 2))) ^ 2 + cos(radians(lat1)) * cos(radians(lat2)) * (sin(radians((lon2 - lon1) / 2))) ^ 2)) as distance
    from
        the_data;
    

    lat2 和 lat1 是您要查找距离的坐标 the_data 是您从中获取值的表名。 这里有几个链接可以帮助您了解它是如何工作的

    Calculate distance between Lat/Lng points

    Mysql Query Explanation of Distance Formula

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多