package dao; /*
思路就是先根據名稱確定經緯度再利用經緯度查詢詳細的地址
*/ 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.alibaba.fastjson.JSONObject; public class Tests { /** * @param addr * 查詢的地址 * @return * @throws IOException */ public String[] getCoordinate(String addr) throws IOException { String lng = null;//經度 String lat = null;//緯度 String address = null; try { address = java.net.URLEncoder.encode(addr, "UTF-8"); }catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } //System.out.println(address); String url = "http://api.map.baidu.com/geocoding/v3/?output=json&ak=你的ak值&coordtype=wgs84ll&address="+address; 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(); if (httpsConn != null) { insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8"); br = new BufferedReader(insr); String data = null; while((data= br.readLine())!=null){ //System.out.println(data); /* * 在這里設置了一個條件判斷,根據百度第圖的返回值表當輸入地址返回值的狀態為‘0’時說明地址查詢發生了錯誤 * 此時得到的經緯度也就是空了 * 所以當結果不為0時就退出返回空值,在循環調用的時候就判斷其是否為空,決定如何進行下一步操作 */ if (data.charAt(10) != '0') { //System.out.println(data.charAt(10)); return null; } JSONObject json = JSONObject.parseObject(data); lng = json.getJSONObject("result").getJSONObject("location").getString("lng"); lat = json.getJSONObject("result").getJSONObject("location").getString("lat"); } } } catch (IOException e) { e.printStackTrace(); } finally { if(insr!=null){ insr.close(); } if(br!=null){ br.close(); } } return new String[]{lng,lat}; } public String[] getAddr(String lng,String lat) throws IOException { // System.out.println(lng ); // System.out.println(lat); //String url = "http://api.map.baidu.com/geocoding/v3/?output=json&ak=你的ak值&coordtype=wgs84ll&location="+lat+","+lng; String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=你的ak值&output=json&coordtype=wgs84ll&location="+lat+","+ lng; URL myURL = null; String province = ""; String city = ""; String qx = ""; String code = ""; URLConnection httpsConn = null; try { myURL = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } InputStreamReader insr = null; BufferedReader br = null; try { httpsConn = (URLConnection) myURL.openConnection();// 不使用代理 if (httpsConn != null) { insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8"); br = new BufferedReader(insr); String data = null; while((data= br.readLine())!=null){ //System.out.println(data); JSONObject json = JSONObject.parseObject(data); province = json.getJSONObject("result").getJSONObject("addressComponent").getString("province"); city = json.getJSONObject("result").getJSONObject("addressComponent").getString("city"); qx= json.getJSONObject("result").getJSONObject("addressComponent").getString("district"); code= json.getJSONObject("result").getJSONObject("addressComponent").getString("adcode"); } } } catch (IOException e) { e.printStackTrace(); } finally { if(insr!=null){ insr.close(); } if(br!=null){ br.close(); } } return new String[]{province,city,qx,code}; } public static void main(String[] args) throws IOException { Tests getLatAndLngByBaidu = new Tests(); String[] o = getLatAndLngByBaidu.getCoordinate("石家庄鐵道大學"); String[] o1 = getLatAndLngByBaidu.getAddr(o[0], o[1]); System.out.println(o1[0]); System.out.println(o1[1]); System.out.println(o1[2]); System.out.println(o1[3]); } }
運行結果: