這篇文章主要介紹了經緯度BL和直角座標XY的正算反算 PHP代碼,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
這裡是用的北京54座標6度分帶。需要用其他座標可以修改參數$_a, $_f 。
經緯度轉XY:
<?phpfunction BLtoXY($latitude, $longitude){ $_a = 6378245.0; $_f = 1.0 / 298.3; $zoneWide = 6; $PI = 3.14159265353846; $iPI = 0.0174532925199433; //3.1415926535898/180.0; //ZoneWide = 6; //6度頻寬 $ProjNo = floor($longitude / $zoneWide); $longitude0 = $ProjNo * $zoneWide + $zoneWide / 2; $longitude0 = $longitude0 * $iPI; $latitude0 = 0; $longitude1 = $longitude * $iPI; //經度轉換為弧度 $latitude1 = $latitude * $iPI; //緯度轉換為弧度 $e2 = 2 * $_f - $_f * $_f; $ee = $e2 * (1.0 - $e2); $NN = $_a / sqrt(1.0 - $e2 * sin($latitude1) * sin($latitude1)); $T = tan($latitude1) * tan($latitude1); $C = $ee * cos($latitude1) * cos($latitude1); $A = ($longitude1 - $longitude0) * cos($latitude1); $M = $_a * ((1 - $e2 / 4 - 3 * $e2 * $e2 / 64 - 5 * $e2 * $e2 * $e2 / 256) * $latitude1 - (3 * $e2 / 8 + 3 * $e2 * $e2 / 32 + 45 * $e2 * $e2 * $e2 / 1024) * sin(2 * $latitude1) + (15 * $e2 * $e2 / 256 + 45 * $e2 * $e2 * $e2 / 1024) * sin(4 * $latitude1) - (35 * $e2 * $e2 * $e2 / 3072) * sin(6 * $latitude1)); $xval = $NN * ($A + (1 - $T + $C) * $A * $A * $A / 6 + (5 - 18 * $T + $T * $T + 72 * $C - 58 * $ee) * $A * $A * $A * $A * $A / 120); $yval = $M + $NN * tan($latitude1) * ($A * $A / 2 + (5 - $T + 9 * $C + 4 * $C * $C) * $A * $A * $A * $A / 24 + (61 - 58 * $T + $T * $T + 600 * $C - 330 * $ee) * $A * $A * $A * $A * $A * $A / 720); $X0 = 1000000 * ($ProjNo + 1) + 500000; $Y0 = 0; $X = round(($xval + $X0) * 100) / 100.0; $Y = round(($yval + $Y0) * 100) / 100.0;return array($X, $Y);}$lng = $_GET["lng"];$lat = $_GET["lat"];$XY = BLtoXY($lat, $lng);echo json_encode($XY);?>
XY轉經緯度:
<?phpfunction XYtoBL($X, $Y){ $_a = 6378245.0; $_f = 1.0 / 298.3; $zoneWide = 6; $PI = 3.14159265353846; $iPI = 0.0174532925199433; //3.1415926535898/180.0; $ProjNo = floor($X / 1000000.0); //尋找帶號 $longitude0 = ($ProjNo - 1) * $zoneWide + $zoneWide / 2; $longitude0 = $longitude0 * $iPI; //中央經線 $X0 = $ProjNo * 1000000 + 500000; $Y0 = 0; $xval = $X - $X0; $yval = $Y - $Y0; //帶內大地座標 $e2 = 2 * $_f - $_f * $_f; $e1 = (1.0 - sqrt(1 - $e2)) / (1.0 + sqrt(1 - $e2)); $ee = $e2 / (1 - $e2); $M = $yval; $u = $M / ($_a * (1 - $e2 / 4 - 3 * $e2 * $e2 / 64 - 5 * $e2 * $e2 * $e2 / 256)); $fai = $u + (3 * $e1 / 2 - 27 * $e1 * $e1 * $e1 / 32) * sin(2 * $u) + (21 * $e1 * $e1 / 16 - 55 * $e1 * $e1 * $e1 * $e1 / 32) * sin(4 * $u) + (151 * $e1 * $e1 * $e1 / 96) * sin(6 * $u) + (1097 * $e1 * $e1 * $e1 * $e1 / 512) * sin(8 * $u); $C = $ee * cos($fai) * cos($fai); $T = tan($fai) * tan($fai); $NN = $_a / sqrt(1.0 - $e2 * sin($fai) * sin($fai)); $R = $_a * (1 - $e2) / sqrt((1 - $e2 * sin($fai) * sin($fai)) * (1 - $e2 * sin($fai) * sin($fai)) * (1 - $e2 * sin($fai) * sin($fai))); $D = $xval / $NN; //計算經度(Longitude) 緯度(Latitude) $longitude1 = $longitude0 + ($D - (1 + 2 * $T + $C) * $D * $D * $D / 6 + (5 - 2 * $C + 28 * $T - 3 * $C * $C + 8 * $ee + 24 * $T * $T) * $D * $D * $D * $D * $D / 120) / cos($fai); $latitude1 = $fai - ($NN * tan($fai) / $R) * ($D * $D / 2 - (5 + 3 * $T + 10 * $C - 4 * $C * $C - 9 * $ee) * $D * $D * $D * $D / 24 + (61 + 90 * $T + 298 * $C + 45 * $T * $T - 256 * $ee - 3 * $C * $C) * $D * $D * $D * $D * $D * $D / 720); //轉換為度 DD $longitude = round(($longitude1 / $iPI) * 1000000) / 1000000.0; $latitude = round(($latitude1 / $iPI) * 1000000) / 1000000.0;return array($longitude, $latitude);}$X = $_GET["X"];$Y = $_GET["Y"];$latlng = XYtoBL($X, $Y);echo json_encode($latlng);?>