Baidu Map API Gets the details of the address based on latitude and longitude

Source: Internet
Author: User

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

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.