import java.net.URL; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /* * 根據經緯度獲取地址:省 市 區 位置名稱 * */ public class AddressUtils { public static void main(String[] args) { Map<String, String> map = getAdd(39.988429, 116.4839); System.out.println(JSON.toJSONString(map)); } /** * 根據經緯度獲取位置信息 * @param latitude 緯度 * @param longitude 經度 * @return */ public static Map<String, String> getAdd(double latitude, double longitude) { // type : 100表明道路,010表明POI(信息點),001表明門址,111能夠同時顯示前三項 String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + latitude + "," + longitude + "&type=010"; String add = ""; try { URL url = new URL(urlString); java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); java.io.BufferedReader in = new java.io.BufferedReader( new java.io.InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { add += line; } in.close(); } catch (Exception e) { e.printStackTrace(); } //System.out.println(add); Map<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = JSONObject.fromObject(add); JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList")); if(jsonArray.size() > 0){ JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0)); String allAdd = j_2.getString("admName"); String[] arr = allAdd.split(","); if(arr.length > 0){ System.out.println("省:" + arr[0] + "\n市:" + arr[1] + "\n區:" + arr[2]); map.put("county", arr[2]);//縣/區 map.put("city", arr[1]);//市 map.put("province", arr[0]);//省 } } return map; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /* * 根據經緯度獲取地址:位置名稱 區 市 省 國家 郵編 * */ public class AddCountryUtils { public final static void main(String[] args) { Map<String, String> map = GetLocationMsg(39.988429, 116.4839); System.out.println(JSON.toJSONString(map)); } /** * 根據經緯度獲取位置信息 * @param latitude 緯度 * @param longitude 經度 * @return */ public static Map<String, String> GetLocationMsg(double latitude, double longitude) { String add = ""; String url = String.format("http://maps.google.cn/maps/api/geocode/json?latlng=%s,%s&language=CN", latitude, 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 InputStreamReader(httpsConn.getInputStream(), "UTF-8"); BufferedReader br = new BufferedReader(insr); String data = null; while ((data = br.readLine()) != null) { add = add + data; } insr.close(); } } catch (IOException e) { e.printStackTrace(); } //System.out.println(add); Map<String, String> map = new HashMap<String, String>(); JSONObject jsonObject = JSONObject.fromObject(add); if (jsonObject.getString("status").equals("OK")) { JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("results")); if (jsonArray.size() > 0) { JSONObject job = JSONObject.fromObject(jsonArray.get(0)); JSONArray jar = JSONArray.fromObject(job.getString("address_components")); if (jar.size() > 0) { for (int i = 0; i < jar.size(); i++) { JSONObject addjob = JSONObject.fromObject(jar.get(i)); String name = addjob.getString("long_name"); System.out.println(name); int j = 0; if(job.getString("formatted_address").contains("郵政編碼")){ j = 1; if(i == jar.size() - 1) map.put("ZipCode", name);//郵編 } if(i == jar.size() - 4 - j) map.put("county", name);//縣/區 if(i == jar.size() - 3 - j) map.put("city", name);//市 if(i == jar.size() - 2 - j) map.put("province", name);//省 if(i == jar.size() - 1 - j) map.put("country", name);//國 } } } } return map; } }
轉載自:http://blog.csdn.net/llllvvv/article/details/77235641java