项目日报(一)

今天进度较慢,就学习利用百度api对地址信息进行了清洗。主要思路就是利用百度地图的搜索功能,通过百度地图获取到该地址的经纬度,然后再通过经纬度获取地址的一个json信息,然后根据分析提取出所在地的省市县三级地址,以及获取到他的行政区划代码。并修改到数据库当中。

下面是我用的一些学习代码:

百度搜索的代码:

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=tEo8ZpyHNgCnDCl0hKt9Wm9hvAIjqua3&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]);
    }
}
View Code

 

posted on 2020-03-12 20:23  一往无前!  阅读(419)  评论(0编辑  收藏  举报