按經緯度搜尋附近的人,並按距離排序的簡單實現。

2021-07-12 06:13:15 字數 1434 閱讀 6308

這是一種簡單的實現,資料量不大的情況下還是能滿足需求的,寫在這裡做乙份記錄。當然也希望有其他更好的方案。

主要思路就是:先以自己的經緯度為中心,計算一定半徑內的方形經緯度邊界,然後用此方形經緯度邊界過濾使用者,並使用乙個計算兩點經緯度之間距離的自定義資料庫函式計算距離,然後按計算得到的距離倒序

long raidus = 10000; //半徑10km

double lat = 23.155778; //當前緯度

double lon = 113.262744; //當前經度

mapparam = new hashmap<>();

param.put("lat", lat);

param.put("lon", lon);

private

void

loadgeosquare(mapparm, double lat, double lon, long raidus);

parm.put("minlat", minlat);

parm.put("minlng", minlng);

parm.put("maxlat", maxlat);

parm.put("maxlng", maxlng);

}

我用的mybatis,當然sql大家應該都能看懂

round(lat_lng_distance(#, #, lat, lon), 2) as distance

from zan_user zuser

where

# and lat < # and lon > # and lon < # ]]>

order by distance asc

limit #,#

其中的 計算兩個經緯度之間距離的 資料庫函式,這裡也貼出來:

create function `lat_lng_distance` (lat1 float, lng1 float, lat2 float, lng2 float)

returns

float

deterministic

begin

return 6371 * 2 * asin

(sqrt(

power(sin((lat1 - abs(lat2)) * pi()/180 / 2),

2) + cos(lat1 * pi()/180 ) * cos(abs(lat2) *

pi()/180) * power(sin((lng1 - lng2) * pi()/180 / 2), 2) ));

end--returns the distance in kilometers, assuming a earth radius of 6,371 km.

以上計算上的數學理論,我沒有深究,也是參看相關資料的。總感覺這種實現方式好搓。

php根據經緯度排序,根據經緯度篩選距離段

sql 語句 select location.from select round 6378.138 2 asin sqrt pow sin 36.668530 pi 180 px lat pi 180 2 2 cos 36.668530 pi 180 cos px lat pi 180 pow si...

跟據經緯度實現附近搜尋Java實現

mysql空間資料庫 矩形演算法 geohash geo演算法 參考文件 一直在琢磨lbs,期待可以發現更好的方案。現在糾結了。簡單列舉一下已經了解到的方案 1.sphinx geo索引 2.mongodb geo索引 3.mysql sql查詢 4.mysql geohash 5.redis ge...

PHP,Mysql根據經緯度計算距離並排序

網上給出的計算公式是這樣的 由大牛改裝過的 是這樣的 mysql排序公式 select from sb user geo order by acos sin city lat.3.1415 180 sin lat 3.1415 180 cos city lat.3.1415 180 cos lat ...