php做附近的人,根據距離由近到遠進行排序

來源:互聯網
上載者:User
關鍵字 php
使用者登陸的時候會擷取到該使用者的經緯度,資料庫中存有所有使用者的經緯度,如何進行由近到遠進行排序,並算出距離

回複內容:

使用者登陸的時候會擷取到該使用者的經緯度,資料庫中存有所有使用者的經緯度,如何進行由近到遠進行排序,並算出距離

可以考慮用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        }      }    }}

}
}

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.