关于thinkphp附近的人经纬度计算方法/距离计算等

小白 2020-07-09 原创文章


很实用的方法,



关于thinkphp附近的人经纬度计算方法/距离计算等 

距离算法我没调用,用的小伙伴 自己调用即可

具体看代码:

/**
 * 选取服务点
 */
public function city(){
    $data = input('post.');
    $latitude = $data['latitude'];//纬度
    $longitude = $data['longitude'];//经度

    $point = $this->returnSquarePoint($longitude,$latitude,50000);        //得到四个点
    $where[] = ['latitude',['>',$point['minLat']],['<',$point['maxLat']],'and'];
    $where[] = ['longitude',['>',$point['minLon']],['<',$point['maxLon']],'and'];
    $where[] = ['pid','NEQ',0];
    $list = CityModel::where($where)->select();
    return json(['code'=>500,'data'=>$list]);
}
// *@param lng float 经度
//  *@param lat float 纬度
//  *@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为单位米
//  *@return array 正方形的四个点的经纬度坐标
public function returnSquarePoint($lng, $lat,$distance)
{
    $PI = 3.14159265;
    $longitude = $lng;
    $latitude = $lat;

    $degree = (24901*1609)/360.0;
    $raidusMile = $distance;

    $dpmLat = 1/$degree;
    $radiusLat = $dpmLat*$raidusMile;
    $minLat = $latitude - $radiusLat;       //拿到最小纬度
    $maxLat = $latitude + $radiusLat;       //拿到最大纬度

    $mpdLng = $degree*cos($latitude * ($PI/180));
    $dpmLng = 1 / $mpdLng;
    $radiusLng = $dpmLng*$raidusMile;
    $minLng = $longitude - $radiusLng;     //拿到最小经度
    $maxLng = $longitude + $radiusLng;     //拿到最大经度
    $range = array(
        'minLat' => $minLat,
        'maxLat' => $maxLat,
        'minLon' => $minLng,
        'maxLon' => $maxLng
    );
    return $range;
}
// 查询距离
public function getDistance($latitude1,$latitude2,$longitude1,$longitude2,$unit){
    $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 $distance;
}


站点信息