地图收索最近的店铺–代码记录

日期:2019-07-09 浏览:143次

代码

/*
 *参数说明:
 *$lng  经度
 *$lat   纬度
 *$distance  周边半径  默认是10公里(0.5Km)
 */
public function returnSquarePoint($lng, $lat, $distance = 10)
{
    $dlng = 2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
    $dlng = rad2deg($dlng);
    $dlat = $distance / 6371;//地球半径,平均半径为6371km
    $dlat = rad2deg($dlat);

    return array(
        'left-top' => array('lat' => $lat + $dlat, 'lng' => $lng - $dlng),
        'right-top' => array('lat' => $lat + $dlat, 'lng' => $lng + $dlng),
        'left-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng - $dlng),
        'right-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng + $dlng)
    );

}

//查询指定最近的体验店铺
public function exper_near()
{
    if ($this->request->isPost()){
        //接收起点经纬度 $longitude, $latitude
        $longitude  = input('post.longitude');
        $latitude   = input('post.latitude');
        /*$scope      = input('post.range');
        if (empty($scope)){
            $scope  = 5;//取店铺的范围
        }*/
        //取值10公里
        $array = $this->returnSquarePoint($longitude, $latitude, 10);
        $map = array(
            'longitude' => array(array('EGT', $array['right-bottom']['lat']), array('ELT', $array['left-top']['lat']), 'and'),
            'latitude' => array(array('EGT', $array['left-top']['lng']), array('ELT', $array['right-bottom']['lng']), 'and'),
        );
        //时间为当天,前端处理
        $data = Db::name('experience_store')->field('longitude,latitude,store_id,store_name,order_nums,regions,address,do_start_time,do_end_time')->where($map)->where('is_del', 0)->limit(10)->select();
        //不存在取值100
        if (count($data) == 0){
            //取值100公里
            $array = $this->returnSquarePoint($longitude, $latitude, 100);
            $map = array(
                'longitude' => array(array('EGT', $array['right-bottom']['lat']), array('ELT', $array['left-top']['lat']), 'and'),
                'latitude' => array(array('EGT', $array['left-top']['lng']), array('ELT', $array['right-bottom']['lng']), 'and'),
            );
            //时间为当天,前端处理
            $data = Db::name('experience_store')->field('longitude,latitude,store_id,store_name,order_nums,regions,address,do_start_time,do_end_time')->where($map)->where('is_del', 0)->limit(10)->select();

        }
        //不存在取值500
        if (count($data) == 0){
            //取值500公里
            $array = $this->returnSquarePoint($longitude, $latitude, 500);
            $map = array(
                'longitude' => array(array('EGT', $array['right-bottom']['lat']), array('ELT', $array['left-top']['lat']), 'and'),
                'latitude' => array(array('EGT', $array['left-top']['lng']), array('ELT', $array['right-bottom']['lng']), 'and'),
            );
            //时间为当天,前端处理
            $data = Db::name('experience_store')->field('longitude,latitude,store_id,store_name,order_nums,regions,address,do_start_time,do_end_time')->where($map)->where('is_del', 0)->limit(10)->select();
        }
        if (count($data) > 0){
            if (count($data) == 1){
                return success('success',$data[0]);
            }
            $tmp    = 0;
            $keys   = 0;
            foreach ($data as  $key => $val){
                $dist = abs($longitude - $val['longitude']) + abs($latitude - $val['latitude']);
                if ($tmp > $dist){
                    $tmp    = $dist;
                    $keys   = $key;
                }
            }
            return success('success', $data[$keys]);
        }else {
            return warn('没有最近的店铺');
        }
    }else{
        return warn('请用post请求');
    }

}

请求事例


没有评论

发表评论