1 <?php 2 //百度坐标转换成GPS坐标 3 $lnglat = '121.437518,31.224665'; 4 function FromBaiduToGpsXY($lnglat){ 5 // 经度,纬度 6 $lnglat = explode(',',$lnglat); 7 list($x,$y) = $lnglat; 8 $Baidu_Server = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x={$x}&y={$y}"; 9 $result = @file_get_contents($Baidu_Server); 10 $json = json_decode($result); 11 if($json->error == 0) 12 { 13 $bx = base64_decode($json->x); 14 $by = base64_decode($json->y); 15 $GPS_x = 2 * $x - $bx; 16 $GPS_y = 2 * $y - $by; 17 return $GPS_x.','.$GPS_y;//经度,纬度 18 } 19 else 20 return $lnglat; 21 } 22 /**********************************************/ 23 function fn_rad($d) 24 { 25 return $d * pi() / 180.0; 26 } 27 // 2点间算法 28 function P2PDistance($latlng1,$latlng2) 29 { 30 // 纬度1,经度1 ~ 纬度2,经度2 31 $latlng1 = explode(',',$latlng1); 32 $latlng2 = explode(',',$latlng2); 33 list($lat1,$lng1) = $latlng1; 34 list($lat2,$lng2) = $latlng2; 35 $EARTH_RADIUS = 6378.137; 36 $radLat1 = fn_rad($lat1); 37 $radLat2 = fn_rad($lat2); 38 $a = $radLat1 - $radLat2; 39 $b = fn_rad($lng1) - fn_rad($lng2); 40 $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2))); 41 $s = $s * $EARTH_RADIUS; 42 $s = round($s * 10000) / 10000; 43 return number_format($s,2); 44 } 45 echo '百度坐标: ',$lnglat,'<br><br>','转换后GPS坐标: ',FromBaiduToGpsXY($lnglat),'<br><br>'; 46 echo '转换前距离: ',P2PDistance('31.224286666667,121.420675','31.224665,121.437518'),'<br/>'; 47 echo '转换后距离: ',P2PDistance('31.224286666667,121.420675','31.220157068379,121.42647022694'); 48 ?>