当前位置 : 主页 > 网络编程 > PHP >

百度地图计算两地现实距离

来源:互联网 收集:自由互联 发布时间:2021-06-28
请自行到百度地图官网申请您的ak $result-result[0]-location-lat, "lng" = $result-result[0]-location-lng ); for ($i=0; $i$length; $i++){ $url = urldecode("http://api.map.baidu.com/cloudgc/v1?ak=您的akaddress=".$param[$i]['addre
请自行到百度地图官网申请您的ak
 


 

 

 

 

 

 

 

 

 

 


  $result->result[0]->location->lat,
            "lng" => $result->result[0]->location->lng
        );
        for ($i=0; $i<$length; $i++){
            $url = urldecode("http://api.map.baidu.com/cloudgc/v1?ak=您的ak&address=".$param[$i]['address']);
            $result = json_decode(get($url));
            $endLatLng = array(
                "lat" => $result->result[0]->location->lat,
                "lng" => $result->result[0]->location->lng
            );
            $distance = array( "distance"=>getDistance($startLatLng,$endLatLng),"unit"=>"m");
            array_push($param[$i],$distance);
        }
        return $param;
    }



//    $add = array(
//        array("address"=>"福建省厦门市集美区"),
//        array("address"=>"北京市海淀区上地十街10号")
//    );
//    print_r(calculateDistance("福建省厦门市集美区",$add));


    /**
     * 发送GET请求
     * @param url
     * @param param
     * @return result
     */

    function get($url, $param=array()){
        if(!is_array($param)){
            throw new Exception("参数必须为array");
        }
        $p='';
        foreach($param as $key => $value){
            $p=$p.$key.'='.$value.'&';
        }
        if(preg_match('/\?[\d\D]+/',$url)){//matched ?c
            $p='&'.$p;
        }else if(preg_match('/\?$/',$url)){//matched ?$
            $p=$p;
        }else{
            $p='?'.$p;
        }
        $p=preg_replace('/&$/','',$p);
        $url=$url.$p;
        //echo $url;
        $http =curl_init($url);
        curl_setopt($http, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($http, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($http,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($http, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        curl_setopt($http, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($http, CURLOPT_HEADER,false);
        $result=curl_exec($http);
        curl_close($http);
        return $result;
    }
    /**
     * 计算两点之间距离
     * @param start
     * @param end
     * @return 米
     */
    function getDistance($startLatLng,$endLatLng){
        if (is_array($startLatLng)&&is_array($endLatLng)){

            $lat1 = (pi()/180)*$startLatLng['lat'];
            $lat2 = (pi()/180)*$endLatLng['lat'];
            $lng1 = (pi()/180)*$startLatLng['lng'];
            $lng2 = (pi()/180)*$endLatLng['lng'];

            //地球半径
            $R =  6371;

            //两点间的距离 km,如果想要米的话,结果*1000就可以了
            $d = acos(sin($lat1)*sin($lat2)+cos($lat1)*cos($lat2)*cos($lng2-$lng1))*$R;
        }else{
            $d = 1;
        }
        return $d*1000;

    }

    /**
     * 通过ip获取地址
     * @param $ip
     * @throws Exception
     * @return $result
     */
    function getAddressByIp($ip){
        if (!filter_var($ip,FILTER_VALIDATE_IP)){
            throw new Exception("IP无效");
        }
        $url = "http://api.map.baidu.com/location/ip?ak=您的ak&coor=bd09ll&ip=".$ip;
        $result = json_decode(get($url));
        if ($result->status != 0){
            throw new Exception("获取坐标失败");
        }
        $url = "http://api.map.baidu.com/geocoder/v2/?location=".$result->content->point->y.",".$result->content->point->x."&ak=您的ak&output=json";

        $result= json_decode(get($url));
        echo $result->result->formatted_address;
        return $result->result->formatted_address;
    }


    //getAddressByIp("120.41.85.225");


/**
 * 通过GPS定位拿到具体位置,电脑只有IE、edge才有定位权限
 * @param $latLng=array('lat'=>?,'lng'=>?)
 * @return $result->result->formatted_address
 */

    function getAddressByGPS($latLng)
    {
        $url = "http://api.map.baidu.com/geocoder/v2/?location=" .$latLng['lat'].",".$latLng['lng']. "&ak=您的ak&output=json";
        $result = json_decode(get($url));
        return $result->result->formatted_address;
    }

    $latLng = array(
        'lat'=>24.609098,
        'lng'=>118.045559
    );
    echo getAddressByGPS($latLng);
网友评论