mysql选择纬度经度距离半径

时间:2016-06-02 09:07:31

标签: mysql

我有两张桌子

首先是“tbl1”

ID  fullName    mobile
1   Aaaa        1234567890
2   Bbbb        9874563210
3   Cccc        1237894560

其次是“tbl2”

ID  lalitude        longtitude      currentTime
2   26.90884600     75.79238500     2016-06-02 13:32:25
2   26.90884600     75.79238500     2016-06-02 13:32:25
1   26.90884600     75.79238500     2016-06-02 13:32:25

我输入了ID = 2lalitude= 28.654490以及longtitude = 77.267117distance = 5 kilometer。 我想从5 kilometer distancelalitude(28.654490)longtitude(77.267117) tbl2 with join tbl1 table获取位于<div style="float: left; margin-bottom: 10px; background-image: url('../bundles/web/img/370799.jpg'); background-repeat: no-repeat; margin-left: 5px; border-radius: 10px 10px 10px 10px; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; padding-left: 10px; padding-top: 5px; padding-bottom: 5px; padding-right: -10px;"> {% for card in cards %} <div id="{{ card.id }}" onclick="getImage({{ card.id }})" style="float:left;"> <img class="img-responsive" style="width: 100px; height: 130px;" src="{{'../bundles/web/img/cartas/' ~ card.image }}"> </div> {% endfor %} </div> 内的用户列表。 我是基于地理信息的新手。

我正在使用MySQL。

2 个答案:

答案 0 :(得分:0)

您可以使用MySQL的spatial extensions并将您的纬度/经度值转换为POINT并使用st_distance() function来计算2点之间的距离。

或者您使用存储为浮点数的纬度/经度值自行计算距离。有关解决方案,请参阅此处的mySQL longitude and latitude query for other rows within x mile radius主题。接受的答案表示如何计算以km为单位的距离。

答案 1 :(得分:0)

这是我的解决方案

DELIMITER ;;
CREATE PROCEDURE proc_getUsersByDistance(in _ID int, in _lat decimal(10,7), in _long decimal(10,7), in _distance int)
BEGIN
    Declare _radius decimal(10,5);
    Declare _lng_min decimal(10,7);
    Declare _lng_max decimal(10,7);
    Declare _lat_min decimal(10,7);
    Declare _lat_max decimal(10,7);

    set _radius=_distance*0.621371;
    set _lng_min = _long - _radius/abs(cos(radians(_lat))*69);
    set _lng_max = _long + _radius/abs(cos(radians(_lat))*69);
    set _lat_min = _lat - (_radius/69);
    set _lat_max = _lat + (_radius/69);

    SELECT tbl1.*, tbl2.latitude, tbl2.longitude,tbl2.currentTime
    from tbl1 INNER JOIN tbl2 on tbl1.ID=tbl2.ID
    where tbl2.ID=_ID and ((tbl2.latitude between _lat_min and _lat_max) and (tbl2.longitude between _lng_min AND _lng_max))
    order by tbl2.currentTime;
END
相关问题