很多情況下,在服務器端處理定位數據的時候,會用到百度地圖等三方的接口,但是在終端上傳的數據是GPS定位數據,這樣會導致兩者誤差很大。我在處理微信定位信息的時候,就遇到這種情況。下面給出一段摘自網絡的轉換代碼,GPS轉換成百度經緯度:
public function getgps($lats, $lngs, $gps = false, $google = false) {//gpg 轉百度座標
$lat = $lats;
$lng = $lngs;
if ($gps)
$c = file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=$lng&y=$lat");
else if ($google)
$c = file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=$lng&y=$lat");
else
return array($lat, $lng);
if (empty($c)) {
return array($lat, $lng);
}
$arr = (array) json_decode($c);
if (!$arr['error']) {
$lat = base64_decode($arr['y']);
$lng = base64_decode($arr['x']);
}
return array($lat, $lng);
}