计算经纬度求出最短的距离

来源:互联网 发布:淘宝带带店卫生带 编辑:程序博客网 时间:2024/05/16 17:39
//求坐标最近的距离public static function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){    $EARTH_RADIUS = 6370.996; // 地球半径系数    $PI = 3.1415926;    $radLat1 = $latitude1 * $PI / 180.0;    $radLat2 = $latitude2 * $PI / 180.0;    $radLng1 = $longitude1 * $PI / 180.0;    $radLng2 = $longitude2 * $PI /180.0;    $a = $radLat1 - $radLat2;    $b = $radLng1 - $radLng2;    $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));    $distance = $distance * $EARTH_RADIUS * 1000;    if($unit==2){        $distance = $distance / 1000;    }    return round($distance, $decimal);}
$ibeacon = explode(',',$ibea_position);  //分割字符串为数组$distance = [];foreach ($goods_position as $key=>$value){    $value = explode(',',$value['goods_position']);  //分割字符串为数组    $distance[$key] = self::getDistance($ibeacon[0],$ibeacon[1],$value[0],$value[1],2,2); //循环对比(返回单位为米)}$pos = array_search(min($distance),$distance); //返回最小的值$recent = $goods_position[$pos];
原创粉丝点击