首页 > 编程知识 正文

百度地和高德地经纬度不一样,百度经纬度转高德经纬度

时间:2023-05-03 23:28:38 阅读:265972 作者:211

if(! function_exists('bdEncrypt')){ //GCJ-02(火星,高德)坐标转换成BD-09(百度)坐标 //@param $longitude 高德经度 //@param $latitude 高德纬度 function bdEncrypt($longitude,$latitude){ $x_pi = 3.14159265358979324 * 3000.0 / 180.0; $x = $longitude; $y = $latitude ; $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi); $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi); $longitude = $z * cos($theta) + 0.0065; $latitude = $z * sin($theta) + 0.006; // 保留小数点后六位 $data['longitude'] = round($longitude, 6); $data['latitude'] = round($latitude, 6); return $data; }}if(! function_exists('gdEncrypt')){ //BD-09(百度)坐标转换成GCJ-02(火星,高德)坐标 //@param $longitude 百度经度 //@param $latitude 百度纬度 function gdEncrypt($longitude,$latitude){ $x_pi = 3.14159265358979324 * 3000.0 / 180.0; $x = $longitude - 0.0065; $y = $latitude - 0.006; $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi); $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi); $longitude = $z * cos($theta); $latitude = $z * sin($theta); $data['longitude'] = round($longitude, 6); $data['latitude'] = round($latitude, 6); return $data; }} PHP根据百度经纬度获取具体的地址

 

/** * [batchLocation description] * @param [type] $locations [description] 经纬度 * @return [type] [description] */public function getBatchLocation( $locations ){$api = config('setting.amap.api.geocode_regeo');//url:https://restapi.amap.com/v3/geocode/regeo$data = ['output' => 'json','location' => $locations,'radius' => 1000,'extensions' => 'all','batch' => 'true','key' => $this->key,//key值];$resp = $this->Curl->get($api, $data);//curl请求return $resp;} 实例调用:$postions = $longitude. ',' .$latitude; //$longitude经度 $latitude纬度$locations = json_decode($this->getBatchLocation(implode('|', $postions)), true);if( $locations['status'] == 1 ) { $locations = $locations['regeocodes']; $address = $locations['formatted_address'];//详细地址 $street = $locations['addressComponent']['township'];//街道 } PHP根据两地经纬度计算距离

 

/** * 求两个已知经纬度之间的距离,单位为米 * * @param lng1 $ ,lng2 经度 * @param lat1 $ ,lat2 纬度 * @return float 距离,单位米 * @author www.Alixixi.com */ public static 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 $s; }示例: $latitude = '30.624762'; $longitude='104.13498'; $latitude1 = '30.676048'; $longitude1='104.144013'; $distance = round($this->getDistance($latitude, $longitude, $latitude1, $longitude1) / 1000, 1);/***方法二*/ function distance($lat1, $lon1, $lat2,$lon2,$radius = 6378.137) { $rad = floatval(M_PI / 180.0); $lat1 = floatval($lat1) * $rad; $lon1 = floatval($lon1) * $rad; $lat2 = floatval($lat2) * $rad; $lon2 = floatval($lon2) * $rad; $theta = $lon2 - $lon1; $dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta)); if ($dist < 0 ) { $dist += M_PI; } return $dist = $dist * $radius; }示例: $latitude = '30.624762'; $longitude='104.13498'; $latitude1 = '30.676048'; $longitude1='104.144013'; $distance = round($this->distance($latitude, $longitude, $latitude1, $longitude1) , 1);

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。