mysql、 php 根據經緯度計算距離
阿新 • • 發佈:2019-02-18
mysql 有個RADIANS 函式 等價於 php裡面的deg2rad 函式
php:
if (!function_exists('get_distance')) { //from ='39.983171,116.308479' function get_distance($from,$to) { $from_arr=explode(',', $from); $to_arr=explode(',', $to); $lat1=$from_arr[0]; $lng1=$from_arr[1]; $lat2=$to_arr[0]; $lng2=$to_arr[1]; $radLat1 = deg2rad($lat1);// deg2rad()函式將角度轉換為弧度 $radLat2 = deg2rad($lat2); $radLng1 = deg2rad($lng1); $radLng2 = deg2rad($lng2); $a = $radLat1 - $radLat2; $b = $radLng1 - $radLng2; $s = 2 * asin(sqrt(pow(sin($a / 2), 2)+cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137; return $s; } }
mysql:
SELECT (2 * asin(sqrt(pow(sin((RADIANS(35.95995) - RADIANS(latitude))/ 2), 2)+cos(RADIANS(35.95995)) * cos(RADIANS(latitude)) * pow(sin((RADIANS(120.19653) -RADIANS(longitude)) / 2), 2))) * 6378.137) as juli FROM fa_circle WHERE id=9;