Get latitude and longitude of a given location according to Baidu Map API

Source: Internet
Author: User

When doing the project, encountered the location to obtain the corresponding latitude and longitude in the map, make notes, for future direct use

Package Com.hpzx.data;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;import com.hpzx.json.httptokener;import com.hpzx.json.jsonexception;/** * Get latitude and longitude through * * @author Jueyue return format:map<string,object> Map map.put ("status", * reader.nextstring ());//Status Map.put ("result", list);//Check Poll result * list<map<string,string>> key: F247CDB592EB43EBAC6CCD27F796E2D2 */public class GetLatAndLngByBaidu {/** * @param addr * Query Address * @return * @throws ioexception */public static string getcoordinate (string addr) th Rows IOException {String address = null;try {address = Java.net.URLEncoder.encode (addr, "UTF-8");} catch (Unsupportedenco Dingexception 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 ();} Httpsconn = (urlconnection) myurl.openconnection ()//Do not use proxy if (httpsconn! = null) {InputStreamReader INSR = NULL;INSR = NE W InputStreamReader (Httpsconn.getinputstream (), "UTF-8"); BufferedReader br = new BufferedReader (INSR); StringBuffer buf = new StringBuffer (); String data = Null;while ((data = Br.readline ()) = null) {Buf.append (Data.replace (":", ""));//System.out.println (data); }try {return Getloc (buf.tostring ());} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} return null;} public static string Getloc (String string) throws Jsonexception {StringBuffer buf = new StringBuffer (); Httptokener x = new Httptokener (string); String t;t = X.nexttoken (), while (X.more ()) {t = X.nexttoken (), if (T.equalsignorecase ("LNG")) {break;}} if (T.equalsignorecase ("LNG")) {Buf.append (X.nexttoken ())), if (X.nexttoken (). equALS ("lat")) {Buf.append (X.nexttoken ());} return buf.tostring ();} return null;} public void Getcoordinate (string longitude, string latitude) {String url = String.Format ("http://api.map.baidu.com/ geocoder?output=json& "+" LOCATION=%S,%S%S&AMP;KEY=F247CDB592EB43EBAC6CCD27F796E2D2 ", Latitude,"%20 ", longitude); URL myurl = null; URLConnection Httpsconn = null;try {myurl = new URL (URL),} catch (Malformedurlexception e) {e.printstacktrace ();} try {httpsconn = (urlconnection) myurl.openconnection (); if (httpsconn! = null) {InputStreamReader INSR = new Inputstreamre Ader (Httpsconn.getinputstream (), "UTF-8"); BufferedReader br = new BufferedReader (INSR); String data = Null;while ((data = Br.readline ()) = null) {System.out.println (data);} Insr.close ();}} catch (IOException e) {e.printstacktrace ();}} public static void Main (string[] args) throws IOException {String str = "Hami South Lake Township Dananhu Mining area, Xinjiang Uygur Autonomous Region"; Getlatandlngbybaidu lotandlng = new Getlatandlngbybaidu (); String result = Getlatandlngbybaidu.getcoordinate (str). REplace (":", ""); SYSTEM.OUT.PRINTLN (result); string[] loc = Result.split (","); Lotandlng.getcoordinate (Loc[0], loc[1]);}}

 

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.