调用百度api利用名称查找该名称的省市县以及行政区划代码

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]); } }

运行结果:

 

posted on 2020-04-07 21:26  一往无前!  阅读(1984)  评论(0编辑  收藏  举报