123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace baidu;
- define('X_PI',3.14159265358979324 * 3000.0 / 180.0);
- class CoordinateTool
- {
- private static $pi = 3.1415926535897932384626;
- private static $a = 6378245.0;
- private static $ee = 0.00669342162296594323;
-
- public static function gcj_bd($gc_loc)
- {
- $x_pi = X_PI;
- $x = $gc_loc['lng'];
- $y = $gc_loc['lat'];
- $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
- $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
- $bd_x = $z * cos($theta) + 0.0065;
- $bd_y = $z * sin($theta) + 0.006;
- $bg_loc = ['lng'=>$bd_x,'lat'=>$bd_y];
- return $bg_loc;
- }
-
- public static function bd_gcj($bd_loc)
- {
- $x_pi = X_PI;
- $x = $bd_loc['lng'] - 0.0065;
- $y = $bd_loc['lat'] - 0.006;
- $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
- $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
- $gc_x = $z * cos($theta);
- $gc_y = $z * sin($theta);
- $gc_loc = ['lng'=>$gc_x,'lat'=>$gc_y];
- return $gc_loc;
- }
-
- public static function wgs_gcj($wgs_loc)
- {
- $wgs_lon = $wgs_loc['lng'];
- $wgs_lat = $wgs_loc['lat'];
- if (self::outOfChina($wgs_lon,$wgs_lat)){
- return $wgs_loc;
- }
- $x_pi = X_PI;
- $dLat = self::transformLat($wgs_lon - 105.0, $wgs_lat - 35.0);
- $dLon = self::transformLon($wgs_lon - 105.0, $wgs_lat - 35.0);
- $radLat = $wgs_lat / 180.0 * self::$pi;
- $magic = sin($radLat);
- $magic = 1 - self::$ee * $magic * $magic;
- $sqrtMagic = sqrt($magic);
- $dLat = ($dLat * 180.0) / ((self::$a * (1 - self::$ee)) / ($magic * $sqrtMagic) * self::$pi);
- $dLon = ($dLon * 180.0) / (self::$a / $sqrtMagic * cos($radLat) * self::$pi);
- $mgLat = $wgs_lat + $dLat;
- $mgLon = $wgs_lon + $dLon;
- $gcj_loc = ['lng'=>$mgLon,'lat'=>$mgLat];
- return $gcj_loc;
- }
-
- public static function gcj_wgs($gcj_loc)
- {
- $wgs_lon = $gcj_loc['lng'];
- $wgs_lat = $gcj_loc['lat'];
- if (self::outOfChina($wgs_lon,$wgs_lat)){
- return $gcj_loc;
- }
- $x_pi = X_PI;
- $dLat = self::transformLat($wgs_lon - 105.0, $wgs_lat - 35.0);
- $dLon = self::transformLon($wgs_lon - 105.0, $wgs_lat - 35.0);
- $radLat = $wgs_lat / 180.0 * self::$pi;
- $magic = sin($radLat);
- $magic = 1 - self::$ee * $magic * $magic;
- $sqrtMagic = sqrt($magic);
- $dLat = ($dLat * 180.0) / ((self::$a * (1 - self::$ee)) / ($magic * $sqrtMagic) * self::$pi);
- $dLon = ($dLon * 180.0) / (self::$a / $sqrtMagic * cos($radLat) * self::$pi);
- $mgLat = $wgs_lat + $dLat;
- $mgLon = $wgs_lon + $dLon;
- $wgsLon = $wgs_lon*2 - $mgLon;
- $wgsLat = $wgs_lat*2 - $mgLat;
- $wgs_loc = ['lng'=>$wgsLon,'lat'=>$wgsLat];
- return $wgs_loc;
- }
-
-
- public static function wgs_bd($wgs_loc){
- $gcj_loc = self::wgs_gcj($wgs_loc);
- $bd_loc = self::gcj_bd($gcj_loc);
- return $bd_loc;
- }
- private static function outOfChina($lon,$lat)
- {
- if ($lon < 72.004 || $lon > 137.8347)
- return true;
- if ($lat < 0.8293 || $lat > 55.8271)
- return true;
- return false;
- }
- private static function transformLat($x,$y)
- {
- $ret = -100.0 + 2.0 * $x + 3.0 * $y + 0.2 * $y * $y + 0.1 * $x * $y + 0.2 * sqrt(abs($x));
- $ret += (20.0 * sin(6.0 * $x * self::$pi) + 20.0 * sin(2.0 * $x * self::$pi)) * 2.0 / 3.0;
- $ret += (20.0 * sin($y * self::$pi) + 40.0 * sin($y / 3.0 * self::$pi)) * 2.0 / 3.0;
- $ret += (160.0 * sin($y / 12.0 * self::$pi) + 320 * sin($y * self::$pi / 30.0)) * 2.0 / 3.0;
- return $ret;
- }
- private static function transformLon($x, $y)
- {
- $ret = 300.0 + $x + 2.0 * $y + 0.1 * $x * $x + 0.1 * $x * $y + 0.1 * sqrt(abs($x));
- $ret += (20.0 * sin(6.0 * $x * self::$pi) + 20.0 * sin(2.0 * $x * self::$pi)) * 2.0 / 3.0;
- $ret += (20.0 * sin($x * self::$pi) + 40.0 * sin($x / 3.0 * self::$pi)) * 2.0 / 3.0;
- $ret += (150.0 * sin($x / 12.0 * self::$pi) + 300.0 * sin($x / 30.0 * self::$pi)) * 2.0 / 3.0;
- return $ret;
- }
- }
|