【PHP版】火星座標系 (GCJ-02) 與百度座標系 (BD-09ll)轉換算法

首先感謝java版做者@宋宋宋偉,java版我是看http://blog.csdn.net/coolypf/article/details/8569813php

 

而後根據java代碼修改爲了php代碼。java

 1     <?php
 2 
 3     /**
 4      * 火星座標系 (GCJ-02) 與百度座標系 (BD-09) 的轉換算法 將 GCJ-02 座標轉換成 BD-09 座標
 5      * 
 6      * @param gg_lat
 7      * @param gg_lon
 8      * @return
 9      */
10     function gcj02_To_Bd09($gg_lon, $gg_lat) {
11         $x = $gg_lon;
12         $y = $gg_lat;
13         $z = Math.sqrt($x * $x + $y * $y) + 0.00002 * Math.sin($y * pi());
14         $theta = Math.atan2($y, $x) + 0.000003 * Math.cos($x * pi());
15         $bd_lon = $z * Math.cos($theta) + 0.0065;
16         $bd_lat = $z * Math.sin($theta) + 0.006;
17         return array($bd_lon, $bd_lat);
18     }
19 
20     /**
21      * 火星座標系 (GCJ-02) 與百度座標系 (BD-09) 的轉換算法   將 BD-09 座標轉換成GCJ-02 座標 
22      * 
23      * @param bd_lon
24      * @param bd_lat
25      * @return
26      */
27     function bd09_To_Gcj02($bd_lon, $bd_lat) {
28         $x = $bd_lon - 0.0065;
29         $y = $bd_lat - 0.006;
30         $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * pi());
31         $theta = atan2($y, $x) - 0.000003 * cos($x * pi());
32         $gg_lon = $z * cos($theta);
33         $gg_lat = $z * sin($theta);
34         return array($gg_lon, $gg_lat);
35     }
36 
37     function coordinate_switch($a,$b){//百度轉騰訊座標轉換
38       $x = (double)$b - 0.0065;
39       $y = (double)$a - 0.006;
40       $x_pi = 3.14159265358979324;
41       $z = sqrt($x * $x+$y * $y) - 0.00002 * sin($y * $x_pi);
42       $theta = atan2($y,$x) - 0.000003 * cos($x*$x_pi);
43       $gb = number_format($z * cos($theta),15);
44       $ga = number_format($z * sin($theta),15);
45       return ['Latitude'=>$ga,'Longitude'=>$gb];
46     }
47      
48     function coordinate_switchf($a,$b){//騰訊轉百度座標轉換
49       $x = (double)$b ;
50       $y = (double)$a;
51       $x_pi = 3.14159265358979324;
52       $z = sqrt($x * $x+$y * $y) + 0.00002 * sin($y * $x_pi);
53       $theta = atan2($y,$x) + 0.000003 * cos($x*$x_pi);
54       $gb = number_format($z * cos($theta) + 0.0065,6);
55       $ga = number_format($z * sin($theta) + 0.006,6);
56      
57       return ['Latitude'=>$ga,'Longitude'=>$gb];
58      
59     }
60 
61     // // 113.139278,23.112388
62     $bd_lon = 23.112388;
63     $bd_lat = 113.139278;
64     // print_r(bd09_To_Gcj02($bd_lon, $bd_lat));
65     print_r(coordinate_switch($bd_lon, $bd_lat));
66     // // 23.106200,113.132840
67     $gg_lon = 23.106200;
68     $gg_lat = 113.132840;
69     // print_r(gcj02_To_Bd09($gg_lon, $gg_lat));
70     print_r(coordinate_switchf($gg_lon, $gg_lat));
71     
相關文章
相關標籤/搜索