亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MySQL大圓距離(Haversine公式)

MySQL大圓距離(Haversine公式)

四季花海 2019-06-01 16:08:03
MySQL大圓距離(Haversine公式)我有一個可以工作的PHP腳本,它獲取經度和緯度值,然后將它們輸入到MySQL查詢中。我只想讓它成為MySQL。下面是我當前的PHP代碼:if ($distance != "Any" && $customer_zip != "") { //get the great circle distance     //get the origin zip code info     $zip_sql = "SELECT * FROM zip_code WHERE zip_code = '$customer_zip'";     $result = mysql_query($zip_sql);     $row = mysql_fetch_array($result);     $origin_lat = $row['lat'];     $origin_lon = $row['lon'];     //get the range     $lat_range = $distance/69.172;     $lon_range = abs($distance/(cos($details[0]) * 69.172));     $min_lat = number_format($origin_lat - $lat_range, "4", ".", "");     $max_lat = number_format($origin_lat + $lat_range, "4", ".", "");     $min_lon = number_format($origin_lon - $lon_range, "4", ".", "");     $max_lon = number_format($origin_lon + $lon_range, "4", ".", "");     $sql .= "lat BETWEEN '$min_lat' AND '$max_lat' AND lon BETWEEN '$min_lon' AND '$max_lon' AND ";     }有誰知道如何把這個變成完整的MySQL嗎?我瀏覽過一些互聯網,但它上的大部分文獻都令人困惑。
查看完整描述

3 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

從…GoogleCodeFAQ-使用PHP、MySQL和GoogleMaps創建一個商店定位器:

這是SQL語句,它將找到距離37,-122坐標半徑25英里內最接近的20個位置。它根據該行的緯度/經度和目標緯度/經度計算距離,然后只請求距離值小于25的行,按距離對整個查詢進行排序,并將其限制為20個結果。要搜索公里而不是英里,請將3959替換為6371。

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * 
sin(radians(lat)) ) ) AS distance 
FROM markers 
HAVING distance < 25 ORDER BY distance 
LIMIT 0 , 20;


查看完整回答
反對 回復 2019-06-01
?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

$greatCircleDistance = acos( cos($latitude0) * cos($latitude1) * cos($longitude0 - $longitude1) + sin($latitude0) * sin($latitude1));

緯度和經度為弧度。

所以

SELECT 
  acos( 
      cos(radians( $latitude0 ))
    * cos(radians( $latitude1 ))
    * cos(radians( $longitude0 ) - radians( $longitude1 ))
    + sin(radians( $latitude0 )) 
    * sin(radians( $latitude1 ))
  ) AS greatCircleDistance 
 FROM yourTable;

是您的sql查詢。

若要獲得以公里或英里為單位的結果,請將結果乘以地球的平均半徑(3959邁爾斯6371公里或3440(海里)

在你的例子中,你正在計算的是一個邊框。如果將坐標數據放在空間啟用MySQL列,你可以用MySQL內置功能查詢數據。

SELECT 
  id
FROM spatialEnabledTable
WHERE 
  MBRWithin(ogc_point, GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))'))


查看完整回答
反對 回復 2019-06-01
  • 3 回答
  • 0 關注
  • 1351 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號