地图 计算2点距离等

Posted *紫色薰衣草*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了地图 计算2点距离等相关的知识,希望对你有一定的参考价值。

    /**
    *计算某个经纬度的周围某段距离的正方形的四个点
    *
    *@param lng float 经度
    *@param lat float 纬度
    *@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
    *@return array 正方形的四个点的经纬度坐标
     用法
     使用此函数计算得到结果后,带入sql查询。
     $squares = returnSquarePoint($lng, $lat);
    */
    private function returnSquarePoint($lng, $lat,$distance = 1){
       $dlng =  2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));
       $dlng = rad2deg($dlng);
       $dlat = $distance/EARTH_RADIUS;
       $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)
        );
    }
    
    //获取2点之间的距离
    private function GetDistance($lat1, $lng1, $lat2, $lng2){
        $radLat1 = $lat1 * (PI / 180);
        $radLat2 = $lat2 * (PI / 180);
        $a = $radLat1 - $radLat2;
        $b = ($lng1 * (PI / 180)) - ($lng2 * (PI / 180));
        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
        $s = $s * EARTH_RADIUS;
        $s = round($s * 10000) / 1000;//10000
        return $s;
    }

 

    //每一次电工打开app会想我传一个坐标位置   用户查看附近的电工时会根据这个经纬度查看附近的电工
    public function ssdg(){
        $only_label = I(‘only_label‘);
        $lat = I(‘lat‘);
        $longs = I(‘longs‘);
        if($lat == "" || $longs == ""){// || $id == ""
            $this->json(‘10003‘,‘参数缺少‘);
        }
        $res = M("user")->where("only_label = ‘$only_label‘")->find();
        if($res){
            $datainfo[‘lat‘] = $lat;
            $datainfo[‘longs‘] = $longs;
            M("user")->where("only_label = ‘$only_label‘")->save($datainfo);
            $this->json(‘10001‘);
        }else{
            $this->json(‘10009‘,‘没有这个电工的id‘);
        }
    }
    
    //用户查看电工的
    public function yhcdg(){
        //纬度
        $lat = I(‘lat‘);
        //经度
        $longs = I(‘longs‘);
        if($lat == "" || $longs == ""){
            $this->json(‘10003‘,‘参数缺少‘);
        }
        $squares = $this->returnSquarePoint($longs, $lat);
        $arr_info = M("user_address")
            ->where("(lat > {$squares[‘right-bottom‘][‘lat‘]} and lat < {$squares[‘left-top‘][‘lat‘]})
            and (longs > {$squares[‘left-top‘][‘lng‘]} and longs < {$squares[‘right-bottom‘][‘lng‘]})")
            ->select();
        foreach ($arr_info as $k=>$v){
            $arr_info[$k][‘juli‘] = $this->GetDistance($lat, $longs, $v[‘lat‘], $v[‘longs‘]);
        }
        $skill_label = M("skill")->select();
        //dump($skill_label);die;
        foreach($skill_label as $key => $val){
            $skl[$val[‘id‘]] = $val[‘name‘];
        }
        //dump($skl);die;
        foreach ($arr_info as $key => $val){
            if($val[‘skill_label‘] == ‘‘){
                $arr_info[$key][‘skill_label‘] = ‘***‘;
            } else {
                $label = explode(‘,‘, $val[‘skill_label‘]);
                foreach($label as $key => $val){
                    $data1[$val[‘id‘]] = $skl[$val];
                }
                foreach ($arr_info as $k=>$v){
                    $arr_info[$k][‘skill_label‘] = implode(‘,‘,$data1);
                }
            }
        }
        foreach($arr_info as $key=> $val){
            $f_id = $val[‘uid‘];
            $wan = M("order")->where("f_id=‘$f_id‘ and statics=‘11‘")->count();//完成总数
            $zcment_sum = M("order_comment")->where("f_id=‘$f_id‘")->sum("zcment");//求和
            $zcment_num = M("order_comment")->where("f_id=‘$f_id‘")->count();//总数
            $zcment = $zcment_sum/$zcment_num;//综合评价
            if($zcment){
                $arr_info[$key][‘zcment‘] = $zcment;
            } else {
                $arr_info[$key][‘zcment‘] = ‘***‘;
            }
            $arr_info[$key][‘dan‘] = $wan;
        }
        //dump($arr_info);die;
        $this->json(10001,array("data"=>$arr_info));
    }

   //电工首页
    public function dgshouye(){
        //用户id
        $uid = I("uid");
        //纬度
        $lat = I(‘lat‘);
        //经度
        $longs = I(‘longs‘);
        if($lat == ""){
            //纬度为空
            $this->json(‘10003‘,‘参数缺少‘);
        }
        if($longs == ""){
            //经度为空
            $this->json(‘10003‘,‘参数缺少‘);
        }
        if($uid == ‘‘){
            //用户为空
            $this->json(‘10003‘,‘参数缺少‘);
        }
        //获取附近0.5千米的所有订单
        $squares = $this->returnSquarePoint($longs, $lat);
        $res = M("order_offer")->where("uid=‘$uid‘")->find();
        if($res){
            $o_id = $res[‘o_id‘];
            //返回的电工坐标附近1千米的所有订单
            $arr_info = M("user_address d")
                    ->join("dian_order o on d.id = o.address_id")
                    ->field("*,o.id o_id")
                    ->where("(d.lat > {$squares[‘right-bottom‘][‘lat‘]} and
                    d.lat < {$squares[‘left-top‘][‘lat‘]}) and
                    (d.longs > {$squares[‘left-top‘][‘lng‘]} and
                            d.longs < {$squares[‘right-bottom‘][‘lng‘]}) and o.id!=‘$o_id‘ and o.statics=1")//and 0.f_id == ‘‘
                    ->select();
        } else {
            //返回的电工坐标附近1千米的所有订单
            $arr_info = M("user_address d")
                    ->join("dian_order o on d.id = o.address_id")
                    ->field("*,o.id o_id")
                    ->where("(d.lat > {$squares[‘right-bottom‘][‘lat‘]} and
                    d.lat < {$squares[‘left-top‘][‘lat‘]}) and
                    (d.longs > {$squares[‘left-top‘][‘lng‘]} and
                            d.longs < {$squares[‘right-bottom‘][‘lng‘]}) and o.statics=1 ")//and 0.f_id == ‘‘
                    ->select();
        }        
        $this->json(10001,array("info"=>$arr_info));
    }

 

以上是关于地图 计算2点距离等的主要内容,如果未能解决你的问题,请参考以下文章

地图上的两点怎样计算距离

使用python计算谷歌地图中2点之间的距离

如何计算百度地图上两点的距离

Python 计算三维空间某点距离原点的欧式距离

高德地图定位,并计算当前定位点和目标点距离

两个纬度/经度点之间的谷歌地图距离计算?