classGps {Private DoubleWglat; Private DoubleWglon; PublicGps (DoubleWglat,DoubleWglon) {Setwglat (Wglat); Setwglon (Wglon); } Public DoubleGetwglat () {returnWglat; } Public voidSetwglat (DoubleWglat) { This. Wglat =Wglat; } Public DoubleGetwglon () {returnWglon; } Public voidSetwglon (DoubleWglon) { This. Wglon =Wglon; } @Override PublicString toString () {returnWglat + "," +Wglon; }}/*** Various coordinate system conversion tool classes *@authorChenfangbo *@returnDouble **/ Public classPositionutil { Public Static FinalString Baidu_lbs_type = "Bd09ll"; Public Static DoublePI = 3.1415926535897932384626 * 3000.0/180.0;//Note here Pi = faction * 3000/180; Public Static DoubleA = 6378245.0; Public Static DoubleEE = 0.00669342162296594323; /*** GCJ-02 to Mars coordinate system (* *) World Geodetic System ==> Mars geodetic system * Earth to Google *@paramlat *@paramLon *@return */ Public StaticGps gps84_to_gcj02 (DoubleLatDoubleLon) { if(Outofchina (lat, lon)) {return NULL; } DoubleDlat = Transformlat (lon-105.0, lat-35.0); DoubleDlon = Transformlon (lon-105.0, lat-35.0); DoubleRadlat = lat/180.0 *Pi; DoubleMagic =Math.sin (Radlat); Magic= 1-ee * Magic *Magic; DoubleSqrtmagic =math.sqrt (Magic); Dlat= (Dlat * 180.0)/((A * (1-ee))/(Magic * sqrtmagic) *pi); Dlon= (Dlon * 180.0)/(A/sqrtmagic * MATH.COS (Radlat) *pi); DoubleMglat = Lat +Dlat; DoubleMglon = lon +Dlon; return NewGps (Mglat, Mglon); } /*** Mars coordinate system (GCJ-02) to * *@paramLon *@paramlat *@return* Google turn to Earth **/ Public StaticGps GCJ_TO_GPS84 (DoubleLatDoubleLon) {GPS GPS=transform (lat, lon); DoubleLontitude = Lon * 2-Gps.getwglon (); DoubleLatitude = lat * 2-Gps.getwglat (); return NewGps (latitude, lontitude); } /*** Mars coordinate system (GCJ-02) and Baidu coordinate system (BD-09) conversion algorithm to convert GCJ-02 coordinates to BD-09 coordinates * Google to Baidu *@paramGg_lat *@paramGg_lon*/ Public StaticGps gcj02_to_bd09 (DoubleGg_lat,DoubleGg_lon) { Doublex = Gg_lon, y =Gg_lat; Doublez = MATH.SQRT (x * x + y * y) + 0.00002 * Math.sin (Y *pi); Doubletheta = math.atan2 (y, x) + 0.000003 * MATH.COS (x *pi); DoubleBd_lon = Z * Math.Cos (theta) + 0.0065; DoubleBd_lat = Z * Math.sin (theta) + 0.006; return NewGps (Bd_lat, Bd_lon); } /*** Baidu coordinate system (BD-09) and Mars coordinate system (GCJ-02) Conversion algorithm * * Convert BD-09 coordinates to GCJ-02 coordinates *@param* Baidu turn to Google*/ Public StaticGps bd09_to_gcj02 (DoubleBd_lat,DoubleBd_lon) { Doublex = bd_lon-0.0065, y = bd_lat-0.006; Doublez = MATH.SQRT (x * x + y * y)-0.00002 * Math.sin (Y *pi); Doubletheta = math.atan2 (y, x)-0.000003 * MATH.COS (x *pi); DoubleGg_lon = Z *Math.Cos (theta); DoubleGg_lat = Z *Math.sin (theta); return NewGps (Gg_lat, Gg_lon); } /*** (BD-09)-->84 * Baidu turn to the earth *@paramBd_lat *@paramBd_lon *@return */ Public StaticGps BD09_TO_GPS84 (DoubleBd_lat,DoubleBd_lon) {Gps gcj02=positionutil.bd09_to_gcj02 (Bd_lat, Bd_lon); Gps map84=positionutil.gcj_to_gps84 (Gcj02.getwglat (), Gcj02.getwglon ()); returnmap84; } Public Static BooleanOutofchina (DoubleLatDoubleLon) { if(Lon < 72.004 | | Lon > 137.8347) return true; if(Lat < 0.8293 | | | lat > 55.8271) return true; return false; } Public StaticGps Transform (DoubleLatDoubleLon) { if(Outofchina (lat, lon)) {return NewGps (lat, lon); } DoubleDlat = Transformlat (lon-105.0, lat-35.0); DoubleDlon = Transformlon (lon-105.0, lat-35.0); DoubleRadlat = lat/180.0 *Pi; DoubleMagic =Math.sin (Radlat); Magic= 1-ee * Magic *Magic; DoubleSqrtmagic =math.sqrt (Magic); Dlat= (Dlat * 180.0)/((A * (1-ee))/(Magic * sqrtmagic) *pi); Dlon= (Dlon * 180.0)/(A/sqrtmagic * MATH.COS (Radlat) *pi); DoubleMglat = Lat +Dlat; DoubleMglon = lon +Dlon; return NewGps (Mglat, Mglon); } Public Static DoubleTransformlat (DoubleXDoubley) {DoubleRET = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x *y+ 0.2 *math.sqrt (Math.Abs (x)); RET+ = (20.0 * Math.sin (6.0 * x * pi) + 20.0 * Math.sin (2.0 * x * pi)) * 2.0/3.0; RET+ = (20.0 * Math.sin (y * pi) + 40.0 * Math.sin (y/3.0 * pi)) * 2.0/3.0; RET+ = (160.0 * Math.sin (y/12.0 * pi) + * * Math.sin (Y * pi/30.0)) * 2.0/3.0; returnret; } Public Static DoubleTransformlon (DoubleXDoubley) {DoubleRET = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 *math.sqrt (Math.Abs (x)); RET+ = (20.0 * Math.sin (6.0 * x * pi) + 20.0 * Math.sin (2.0 * x * pi)) * 2.0/3.0; RET+ = (20.0 * Math.sin (x * pi) + 40.0 * Math.sin (x/3.0 * pi)) * 2.0/3.0; RET+ = (150.0 * Math.sin (x/12.0 * pi) + 300.0 * Math.sin (x/30.0 * pi)) * 2.0/3.0; returnret; } Public Static voidMain (string[] args) {//the latitude and longitude obtained by the Beidou chip is WGS84 geographical coordinates 31.426896,119.496145GPS GPS =NewGps (30.6511, 103.681568); //System.out.println ("GPS:" + GPS); Gps GCJ =gps84_to_gcj02 (Gps.getwglat (), Gps.getwglon ()); System.out.println ("Earth goes to Google:" +GCJ); GPS BD=gcj02_to_bd09 (Gcj.getwglat (), Gcj.getwglon ()); System.out.println ("Google turn Baidu:" +BD); Gps gcj2=bd09_to_gcj02 (Bd.getwglat (), Bd.getwglon ()); System.out.println ("Baidu turn to Google:" +gcj2); Gps Star=gcj_to_gps84 (Gcj.getwglat (), Gcj.getwglon ()); System.out.println ("Google turns to Earth:" +star); Gps Gcj3=bd09_to_gps84 (Gps.getwglat (), Gps.getwglon ()); System.out.println ("Baidu turns to Earth:" +gcj3); } }
Various map coordinate system conversion tools