一、需求场景
之前开发中对数据加工的时候,需要知道数据的地市、区县的名称。但是大部分数据只有地址和企业名称,并没有其他特殊的标志。根据地址来处理地市州的话,有明显的市州的信息的话还能根据码表进行匹配。但是还存在没有地市和区县的情况。在这种情形下,我第一想到的是根据地图开放API来获取对应信息。(谷歌和百度)
二、关于百度地图API
百度地图经过多年的发展,在当前大数据背景下对于地址信息的处理能力应当是不错的。要申请百度地图API接口,首先得注册一个百度账号。登录到百度地图开发平台:http://lbsyun.baidu.com/
首先在开API控制台创建一个应用
创建应用的时候需要选择服务端、安卓等。因为我是服务端开发,选择服务端即可。然后应用对IP有限制规则(防止恶意访问),页面底部也有对IP等信息
创建成功之后又一个AK,这个是访问API的唯一口令(如果设置了IP则还有IP)
以上就是关于百度API 的简要使用规则,如果要进行更深层次的操作你的账户还需要绑定百度开发者中心。(需要进行开发者认证)
三、根据百度API处理实际问题
根据百度API给出的实际功能,需要使用Geocoding API进行操作。具体操作如下:
1.熟悉API
API首先给出了根据地址查出经纬度的实例:
http://api.map.baidu.com/geocoder/v2/?address=北京市海淀区上地十街10号&output=json&ak=E4805d16520de693a3fe707cd
我们先明确的我们需要访问的服务地址是:
http://api.map.baidu.com/geocoder/v2/
然后必要的参数是ak,address是我们需要查询的地址,输出根据json输出(可选xml)。具体的介绍可以看API文档说明
第二个功能则是根据经纬度来获取地址信息
http://api.map.baidu.com/geocoder/v2/?location=39.983424,116.322987&output=json&ak=您的ak
可以看到必要参数是location,这里注意的是经纬度的顺序。必要参数也是ak
返回的数据格式详见API手册
2.实际应用示例代码
- 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/geocoder/v2/?output=json&ak=您的ak&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){
- 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 {
- String url = "http://api.map.baidu.com/geocoder/v2/?output=json&ak=您的ak&location="+lat+","+lng;
- URL myURL = null;
- String city = "";
- String qx = "";
- 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){
- JSONObject json = JSONObject.parseObject(data);
- city = json.getJSONObject("result").getJSONObject("addressComponent").getString("city");
- qx= json.getJSONObject("result").getJSONObject("addressComponent").getString("district");
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(insr!=null){
- insr.close();
- }
- if(br!=null){
- br.close();
- }
- }
- return new String[]{city,qx};
- }
- 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]);
- }
- }
可以根据上面的代码进行封装,如果需要在正式编码环境中使用。建议先判断json返回的数据状态,然后再获取其中的值。
注意:
url中部分字符需要进行编码(详细信息请参考API文档)
在对地址数据进行经纬度查询的时候,尽量保证地址唯一
折腾API的时候注意调用次数(使用次数每天有限制)
点关注,不迷路
文章每周持续更新,可以微信搜索「 十分钟学编程 」第一时间阅读和催更,如果这个文章写得还不错,觉得有点东西的话 ~求点赞👍 求关注❤️ 求分享❤️
各位的支持和认可,就是我创作的最大动力,我们下篇文章见!