求两个已知经纬度之间的距离,单位为米geo

php

浏览数:445

2019-1-8

 0 && $s <= 1000){
        return $s.'m';
    }elseif($s > 1000 && $s <= 2000){
        return '<2km';
    }elseif($s > 2000 && $s <= 3000){
        return '<3km';
    }elseif($s > 3000 && $s <= 4000){
        return '<4km';
    }elseif($s > 4000 && $s <= 5000){
        return '<5km';
    }elseif($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;
    }