接口获取行政区划代码_调用百度api利用名称查找该名称的省市县以及行政区划代码...

packagedao;

/*

思路就是先根据名称确定经纬度再利用经纬度查询详细的地址

*/importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.UnsupportedEncodingException;importjava.net.MalformedURLException;importjava.net.URL;importjava.net.URLConnection;importcom.alibaba.fastjson.JSONObject;public classTests {/***@paramaddr

* 查询的地址

*@return*@throwsIOException*/

public String[] getCoordinate(String addr) throwsIOException {

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= newURL(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= newBufferedReader(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 newString[]{lng,lat};

}public String[] getAddr(String lng,String lat) throwsIOException {//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= newURL(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= newBufferedReader(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 newString[]{province,city,qx,code};

}public static void main(String[] args) throwsIOException {

Tests getLatAndLngByBaidu= newTests();

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值