Baidu coordinate conversion GPS coordinate tool class

Source: Internet
Author: User

Baidu coordinates and the standard GPS coordinates have the deviation, here provides a Baidu coordinates to convert the GPS coordinates the tool class.

Package Com.smarteye.baidumap;import Com.baidu.location.bdlocation;public class Globaltool {public final static double A = 6378245.0;public final static double EE = 0.00669342162296594323;//Determines whether the coordinates are in China public static Boolean Outofchina (Bdlocati On bdlocation) {Double lat = bdlocation.getlatitude ();d ouble lon = Bdlocation.getlongitude (); if (Lon < 72.004 | | Lon &G T 137.8347) return true;if (Lat < 0.8293 | | lat > 55.8271) return true;if ((119.962 < lon && Lon < 121.750 ) && (21.586 < lat && lat < 25.463)) return True;return false;} Public final static Double x_pi = 3.14159265358979324 * 3000.0/180.0;public static bdlocation baidu_to_wgs84 (bdlocation Bdlocation) {if (Outofchina (bdlocation)) {return bdlocation;}  Double x = Bdlocation.getlongitude ()-0.0065;double y = bdlocation.getlatitude ()-0.006;double z = math.sqrt (x * x + y * Y)-0.00002 * Math.sin (y * x_pi);d ouble theta = math.atan2 (y, x)-0.000003 * MATH.COS (x * x_pi); bdlocation.setlongitude (Z * Math.Cos (theta)); Bdlocation.setlatitude (Z * Math.sin (theta)); return gcj02_to_wgs84 (bdlocation);} public static bdlocation gcj02_to_wgs84 (Bdlocation bdlocation) {if (Outofchina (bdlocation)) {return bdlocation;} Bdlocation tmplocation = new Bdlocation (); Tmplocation.setlatitude (Bdlocation.getlatitude ()); Tmplocation.setlongitude (Bdlocation.getlongitude ()); Bdlocation tmplatlng = wgs84_to_gcj02 (tmplocation);d ouble Tmplat = 2 * bdlocation.getlatitude ()-TmpLatLng.getLatitude ( );d ouble tmplng = 2 * bdlocation.getlongitude ()-tmplatlng.getlongitude (); for (int i = 0; i < 0; ++i) {Tmplocation.setl Atitude (Bdlocation.getlatitude ()); Tmplocation.setlongitude (Bdlocation.getlongitude ()); tmpLatLng = WGS84_to_GCJ02 (tmplocation); Tmplat = 2 * tmplat-tmplatlng.getlatitude (); tmplng = 2 * Tmplng-tmplatlng.getlongitude ();} Bdlocation.setlatitude (Tmplat); Bdlocation.setlongitude (TMPLNG); return bdlocation;} public static bdlocation wgs84_to_gcj02 (Bdlocation bdlocation) {if (Outofchina (bdlocation)) {returnBdlocation;} Double Dlat = Transformlat (Bdlocation.getlongitude ()-105.0,bdlocation.getlatitude ()-35.0);d ouble Dlon = TransformLon (Bdlocation.getlongitude ()-105.0,bdlocation.getlatitude ()-35.0);d ouble Radlat = Bdlocation.getlatitude ()/180.0 * Ma Th. Pi;double Magic = Math.sin (Radlat); magic = 1-ee * Magic * magic;double sqrtmagic = math.sqrt (Magic);d lat = (Dlat * 180.0 )/((A * (1-ee))/(Magic * sqrtmagic) * Math.PI);d lon = (Dlon * 180.0)/(A/sqrtmagic * MATH.COS (Radlat) * Math.PI); b Dlocation.setlatitude (Bdlocation.getlatitude () + Dlat); Bdlocation.setlongitude (Bdlocation.getlongitude () + DLon); return bdlocation;} public static double Transformlat (double x, double y) {double ret = -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 * Math.PI) + 20.0 * Math.sin (2.0 * x* Math.PI)) * 2.0/3.0  ; ret + = (20.0 * Math.sin (Y * math.pi) + 40.0 * Math.sin (y/3.0* math.pi)) * 2.0/3.0;ret + = (160.0 * Math.sin (y/12.0 * MAth. PI) + * Math.sin (y* math.pi/30.0) * 2.0/3.0;return ret;}  public static double Transformlon (double x, double y) {double ret = 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 * Math.PI) + 20.0 * Math.sin (2.0 * x* Math.PI)) * 2.0/3.0;ret + = (20.0 * Math.sin (x * math.pi) + 40.0 * Math.sin (x/3.0* math.pi)) * 2.0/3.0;ret + = (150.0 * Math.sin (x/12.0 * math.p) I) + 300.0 * Math.sin (x/30.0 * math.pi)) * 2.0/3.0;return ret;}}


Baidu coordinate conversion GPS coordinate tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.