Baidu Map API server to get latitude and longitude according to the address

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;
Import Java.util.HashMap;
Import Java.util.Map;

Import Com.alibaba.fastjson.JSONObject;

public class Getaddressbybaidu {
public static map<string, object> getcity (String lngs,string lats) throws IOException {
String LNG = null;
String lat = null;
map<string, object> map = new hashmap<string, object> ();
try {
lat = Java.net.URLEncoder.encode (lats, "UTF-8");
LNG = Java.net.URLEncoder.encode (lngs, "UTF-8");
} catch (Unsupportedencodingexception E1) {
E1.printstacktrace ();
}
String Lngandlat = lat+ "," +LNG;
String key = "Your Key";
String url = String.Format ("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s&output=json&pois= 1 ", key, Lngandlat);
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;
String country = "";
String province = "";
String city = "";
String district = "";
String street = "";
String street_number = "";
while ((Data= br.readline ())!=null) {
Jsonobject para = jsonobject.parseobject (data);
if (para!=null) {
String result = para.getstring ("result");
Jsonobject paras = jsonobject.parseobject (result);
if (paras!=null) {
String addresscomponent = paras.getstring ("addresscomponent");
Jsonobject PARASL = Jsonobject.parseobject (addresscomponent);
System.out.println (Parasl.tostring ());
if (parasl!=null) {
Country = parasl.getstring ("Country");
Province = parasl.getstring ("province");
City = parasl.getstring ("city");
district = parasl.getstring ("district");
Street = parasl.getstring ("Street");
Street_number = parasl.getstring ("Street_number");
}
}
}
}
Map.put ("Country", country);
Map.put ("province", province);
Map.put ("City", city);
Map.put ("district", district);
Map.put ("Street", street);
Map.put ("Street_number", Street_number);
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (insr!=null) {
Insr.close ();
}
if (br!=null) {
Br.close ();
}
}
return map;
}
public static void Main (string[] args) throws IOException {
map<string, object> detail = getcity ("121.378452", "37.527401");
System.out.println (Detail.get ("country") + "," +detail.get ("province") +detail.get ("District") +detail.get ("Street") ) +detail.get ("Street_number"));
}
}

Baidu Map API server to get latitude and longitude according to the address

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.