Package Com.haiyisoft.cassistant.mapapi;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.unsupportedencodingexception;import java.net.MalformedURLException; Import Java.net.url;import java.net.URLConnection; /** * Get latitude and longitude * * @author Sunny * Key: F247CDB592EB43EBAC6CCD27F796E2D2 */public class Getlatandlngbybaidu {/** * @param addr * Query Address * @return * @throws ioexception * * Public object[] Getcoordinate (String addr) throws IOException {string LNG = n ull;//Longitude string lat = null;//Latitude string address = null; try {address = Java.net.URLEncoder.encode (addr, "UTF-8");} catch (Unsupportedencodingexception E1) {e1.printstacktrace ();} String key = "QDAWS0IFG7VID98VWSDCYZVR8J6FRKGV"; String url = string. Format ("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s", address, key) ; URL myurl = null; URLConnection httpsconn = null; try {myurl = new URL (URL),} catch (Malformedurlexception e) {e.printstacktrace ();} InputstreaMreader INSR = null; BufferedReader br = null;try {httpsconn = (urlconnection) myurl.openconnection ();//Do not use proxy if (httpsconn! = null) {INSR = New InputStreamReader (Httpsconn.getinputstream (), "UTF-8"); br = new BufferedReader (INSR); String data = null; int count = 1;while ((data= br.readline ())!=null) {if (count==5) {LNG = (String) data.subsequence (Data.indexof (":") +1, Data.indexof (","));//longitude count++;} else if (count==6) {lat = data.substring (Data.indexof (":") +1);//latitude count++;} else{count++;}} }} catch (IOException e) {e.printstacktrace ();} finally {if (insr!=null) {insr.close ();} if (br!=null) {br.close ();}} return new Object[]{lng,lat}; } public static void Main (string[] args) throws IOException {Getlatandlngbybaidu Getlatandlngbybaidu = new Getlatandlngbyb Aidu (); object[] o = getlatandlngbybaidu.getcoordinate ("Oriental Electronics, No. 2nd, Yantai Airport Road, Shandong Province"); System.out.println (o[0]);//Longitude System.out.println (o[1]);//Latitude}}
Baidu Map API obtains address details based on latitude and longitude