1 protected void runtest () throws throwable {2 3 try {4 baidulocation BL = new baidulocation (); 5 BL. gpsx = 120; // longitude 6 BL. gpsy = 30; // latitude 7 getbaidulocation (BL); 8 If (BL. OK) {9 int baidux = (INT) (BL. baidux * 1e6); 10 int baiduy = (INT) (BL. baiduy * 1e6); 11 // The conversion is successful. The coordinates are 12} 13 else {14 // Conversion failed 15} 16} 17 catch (exception ex) {18} 19} 20 21 class baidulocation {22 public float gpsx, gpsy; 23 public fl Oat baidux, baiduy; 24 public Boolean OK = false; 25} 26 27 public static string getbaidulocation (float X, float y) throws malformedurlexception, ioexception {28 string url = string. format ("http://api.map.baidu.com/ag/coord/convert? From = 0 & to = 4 & X = % F & Y = % F ", x, y); 29 httpurlconnection urlconnection = (httpurlconnection) (new URL (URL ). openconnection (); 30 urlconnection. connect (); 31 bufferedreader reader = new bufferedreader (New inputstreamreader (urlconnection. getinputstream (); 32 string lines = reader. readline (); 33 reader. close (); 34 urlconnection. disconnect (); 35 return lines; 36} 37 38 public static Boolean getbaidulocation (baidulocation BL) {39 try {40 BL. OK = false; 41 string res = getbaidulocation (BL. gpsx, BL. gpsy); 42 if (res. startswith ("{") & res. endswith ("}") {43 res = res. substring (1, Res. length ()-2 ). replace ("\" "," "); 44 string [] lines = res. split (","); 45 for (string line: lines) {46 string [] items = line. split (":"); 47 If (items. length = 2) {48 if ("error ". equals (items [0]) {49 BL. OK = "0 ". equals (items [1]); 50} 51 if ("X ". equals (items [0]) {52 BL. baidux = convertbase64 (items [1]); 53} 54 if ("Y ". equals (items [0]) {55 BL. baiduy = convertbase64 (items [1]); 56} 57} 58} 59} 60} catch (exception e) {61 BL. OK = false; 62} 63 return BL. OK; 64} 65 Private Static float convertbase64 (string Str) {66 byte [] BS = base64.decode (STR); 67 return float. valueof (new string (BS); 68}
Longitude and latitude conversion interface http://map.yanue.net/gps.html
Ox home: http://map.yanue.net/
Original GPS longitude and latitude to Baidu longitude and latitude