根据经度纬度计算两者之间的距离
生活随笔
收集整理的這篇文章主要介紹了
根据经度纬度计算两者之间的距离
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
$i = 10.0; //差值可自定義,值越大,范圍就越大
$min_latitude = $lat - $i; //緯度最小值
$max_latitude = $lat + $i; //緯度最大值
$min_longitude = $lng - $i; //經度最小值
$max_longitude = $lng + $i; //經度最大值
where('(c.shop_address_x BETWEEN '.$min_latitude.' AND '.$max_latitude.') AND (c.shop_address_y BETWEEN '.$min_longitude.' AND '.$max_longitude.')')//根據店鋪距離用戶的遠近進行從近到遠的排序
?
getDistance($value['shop_address_x'],$value['shop_address_y'],$lat,$lng).'km';//根據用戶經度緯度和店鋪的經度緯度 計算兩者之間的距離 /** * 計算兩組經緯度坐標 之間的距離 * params :lat1 緯度1; lng1 經度1; lat2 緯度2; lng2 經度2; len_type (1:m or 2:km); * return m or km */ function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 2, $decimal = 2) {$earth_radius = 6378.137;$pi = 3.1415926;// define('EARTH_RADIUS', 6378.137);//地球半徑 // define('PI', 3.1415926);$radLat1 = $lat1 * $pi / 180.0; $radLat2 = $lat2 * $pi / 180.0; $a = $radLat1 - $radLat2; $b = ($lng1 * $pi / 180.0) - ($lng2 * $pi / 180.0); $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))); $s = $s * $earth_radius; $s = round($s * 1000); if ($len_type > 1) { $s /= 1000; } return round($s, $decimal); }?
總結
以上是生活随笔為你收集整理的根据经度纬度计算两者之间的距离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 那些曾让你哭过的事
- 下一篇: 考试末名的英国教授获诺贝尔奖