使用者登陸的時候會擷取到該使用者的經緯度,資料庫中存有所有使用者的經緯度,如何進行由近到遠進行排序,并算出距離
回複内容:
使用者登陸的時候會擷取到該使用者的經緯度,資料庫中存有所有使用者的經緯度,如何進行由近到遠進行排序,并算出距離
可以考慮用GeoHASH實作,效率更高,參考這篇http://www.cnblogs.com/LBSer/p/3310455.html
我覺得是時候把這個位址貼上來了。
https://segmentfault.com/q/1010000000345194
用php+redis可以實作 可以參考這個 http://www.wubiao.info/401
PHP安裝GeoIP擴充和資料庫根據IP擷取訪客所在國家/城市/經緯度等資訊
然後就可以用geoip_record_by_name($_SERVER['REMOTE_ADDR'])擷取使用者經緯度.
注意:geoip_record_by_name()傳回的西經和南緯是負數.
5000米轉成經緯度:
緯度 Latitude: 1 deg = 110852 m
經度 Longitude: 1 deg = 111320*cos(lat) m
同一經線上,相差一緯度約為 110852 米
同一緯線上,相差一經度約為 111320*cos(lat) 米 (lat為該緯線的緯度)
($lat-$y) and lat < ($lat+$y) and
lon > ($lon-$x) and lon < ($lon+$x);
';
這個範圍是一個粗略的範圍,後面距離計算後把超過5公裡的使用者篩掉即可.
根據上面查詢出來的使用者的經緯度,
用半正矢公式根據經緯度計算兩點間距離:
elasticsearch 得距離搜尋,擷取最近的20公裡資料
GET test/test/_search
{
"query": {
"filtered": {
"query" : {
"match_all" : {}
},
"filter": {
"geo_distance": {
"distance": 20,
"distance_unit": "km",
"location": {
"lat" : 40.11116,
"lon" : -71.34115
}
}
}
}
}
}
相關标簽:php
本文原創釋出php中文網,轉載請注明出處,感謝您的尊重!