PHP地理位置计算

2020-12-22 09:27

阅读:584

标签:show   sea   pow   phone   convert   函数   获取   name   time   

/**
*求两个已知经纬度之间的距离,单位为米
*@param lng1,lng2 经度
*@param lat1,lat2 纬度
*@return float 距离,单位米
**/
function getdistance($lng1,$lat1,$lng2,$lat2){
    //将角度转为狐度
    $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*1000;
    return intval($s);
}

function distanceConvert($s){
    $s = intval($s);
    if($s  0 && $s  1000 && $s  2000 && $s  3000 && $s  4000 && $s  5000){
        return ‘>5km‘;
    }
}

/**
* 根据条件获取商户列表
* @param string $lng
* @param string $lat
* @param string $city_id
* @param string $district_id
* @param string $type_id
* @param string $search
* @return mixed
*/
//如果存在经纬度则考虑范围5公里内数据,如果不存在经纬度,则不考虑
public function GetShopList($navigationId = 0,$page = 1,$size =10 ,$lng = ‘‘, $lat =‘‘, $city_id = ‘‘,$district_id = ‘‘,$type_id = ‘‘,$search = ‘‘)
{
    $field = ‘id,province_id,city_id,district_id,navigation_id,type_id,name,phone,address,logo,stime,etime,rule,activity,discount,latitude,longitude,status,token,sort,description,gmt_created‘;

    $where = array(
        ‘is_deleted‘ => 0,
        ‘is_show‘ => 1,
        ‘navigation_id‘=>$navigationId,
    );

    //判断城市,县区,分类是否存在
    if(!empty($city_id)){
        $where[‘city_id|province_id‘] = $city_id;
    }
    if(!empty($district_id)){
        $where[‘district_id‘] = $district_id;
    }
    if(!empty($type_id)){
        $where[‘type_id‘] = $type_id;
    }
    if(!empty($search)){
        $where[‘name‘] = array(‘like‘,"%{$search}%");
    }

    $offset = ($page * $size) - ($size - 1);
    //如果存在经纬度,就根据经纬度查询5公里范围内数据
    if(!empty($lng) && !empty($lat)){
        // //求出半径
        // $radius = 5;//半径范围,单位km
        // $rangeLat = 180 / pi() * $radius / 6372.797;//纬度范围
        // $rangeLng = $rangeLat / cos($lat * pi() / 180.0); //经度范围
        // $maxLat = $lat + $rangeLat; //x1
        // $minLat = $lat - $rangeLat; //x0
        // $maxLng = $lng + $rangeLng; //y1
        // $minLng = $lng - $rangeLng; //y0
        //
        // $where[‘latitude‘] = array(array(‘lt‘,$maxLat),array(‘gt‘,$minLat));
        // $where[‘longitude‘] = array(array(‘lt‘,$maxLng),array(‘gt‘,$minLng));

        $list = $this->where($where)->field($field)->order("abs(longitude - {$lng}) + abs(latitude - {$lat})")->limit($offset,$size)->select();
    }else{
    $list = $this->where($where)->field($field)->order(‘sort asc‘)->limit($offset,$size)->select();
    }

    //记录错误日志
    if ($list === false) {
        wLog(‘GetShopList error! sql:‘ . $this->getLastSql(), $this->errorFile);
        wLog(‘GetShopList error! errorInfo:‘ . $this->error, $this->errorFile);
    }
    //wLog(‘GetShopList sql:‘ . $this->getLastSql(), $this->errorFile);
    return $list;
}

  

PHP地理位置计算

标签:show   sea   pow   phone   convert   函数   获取   name   time   

原文地址:https://www.cnblogs.com/seanpan/p/13993384.html


评论


亲,登录后才可以留言!