首先感谢java版作者@宋宋宋伟,java版我是看http://blog.csdn.net/coolypf/article/details/8569813
然后根据java代码修改成了php代码。
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