Java according to Baidu API to obtain latitude and longitude, and then according to latitude and longitude in obtaining city information

Source: Internet
Author: User

Original: http://www.open-open.com/code/view/1421032487812

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 jueyue return format:map<string,object> Map map.put ("status", * reader.nextstring ());//Status Map.put ("Re Sult ", list);//Query result * list<map<string,string>> * key: F247CDB592EB43EBAC6CCD27F796E2D2 */public class  Getlatandlngbybaidu {/** * @param addr * Query Address * @return * @throws IOException */Public        Object[] Getcoordinate (String addr) throws IOException {string lng = null;//Longitude string lat = null;//latitude         String address = null;         try {address = Java.net.URLEncoder.encode (addr, "UTF-8");         }catch (unsupportedencodingexception E1) {e1.printstacktrace ();         } String key = "F247CDB592EB43EBAC6CCD27F796E2D2"; 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.subsequenc                    E (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 G        Etlatandlngbybaidu ();        object[] o = Getlatandlngbybaidu.getcoordinate ("Tianfu Four Street, Chengdu"); System.out.println (o[0]);//Longitude System.out.println (o[1]);//Latitude}}

Output results: 104.04701

30.548397

Call the following path to get an address based on latitude and longitude, return data in JSON format,

http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=30.548397,104.04701&output= Json&pois=1

return Result:

{"Status": 0, "result": {"location": {"LNG": 104.04701001273, "lat": 30.548396923974}, "Formatted_address": " Tianfu Four Street, Wuhou District, Chengdu City, Sichuan Province, "Business": "", "addresscomponent": {"City": "Chengdu", "District": "Wuhou", "Province": "Sichuan Province", "Street": "Tianfu four Street "," Street_number ":" "}," POIs ": [{" addr ":" Tianfu four Street, Wuhou District, Chengdu City, Sichuan Province "," CP ":" NavInfo "," Distance ":" 144 "," Name ":" Poly Heart Language Garden "," Poitype ":" Estate Community "," point ": {" x ": 104.04823889466," y ": 30.54874673449}," tel ":" "," UID ":" 881965b85a8d8a977369d348 "," Zip ":" "},{" addr ": Tianfu four Street, Wuhou District, Chengdu City, Sichuan Province", "CP": "NavInfo", "Distance": "629", "name": "Yi du international", "Poitype": "Estate Community", "point": {"x" : 104.05247889667, "y": 30.547152788724}, "tel": "", "UID": "e49f869c4f156be9c6effb6b", "Zip": "},{" addr ":" Jian nan da Dao, Wuhou District, Chengdu City, Sichuan Province, "CP": "NavInfo", "Distance": "881", "Name": "Plaza", "Poitype": "Estate Community", "point": {"x": 104.05252381195, "Y" : 30.553318490823}, "tel": "", "UID": "a2a7b0b167a0e1997adbd206", "Zip": "},{" addr ": De sai Three street, Wuhou District, Chengdu City, Sichuan Province", "CP": "NavInfo", " Distance ":" 989 "," name ":" Customer Parking "," Poitype ":" Traffic Facilities "," point ": {" x ": 104.04942465794," y ": 30.555798655385}," tel ":" "," UID ":" D135a6e15571911483a00cfe "," Zip ":" "}],"Citycode": 75}} 

Java according to Baidu API to obtain latitude and longitude, and then according to latitude and longitude in obtaining city information

Related Article

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.