关键代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
public static String addressResolution(String address){
        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县|.+区|.+市)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)";
        Matcher m=Pattern.compile(regex).matcher(address);
        String province=null,city=null,county=null;
        String str="";
        while(m.find()){
            province=m.group("province");
            city=m.group("city");
            county=m.group("county");
            str = province + city + county;
        }
        return str;
    }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public static String getlocation1(String loc){
        String location2="";
          String url="http://api.map.baidu.com/geocoding/v3/?address="+loc+"10号&output=xml&ak=ld0uqubVfSTAUlXH5qIMN2F3Snsp16LU&callback=showLocation";
          //System.out.println(url);
          Document doc = null;
           java.net.HttpURLConnection conn = null;
           InputStream ins = null;
           SAXReader reader = null;
           try{
            //HttpTimeoutHandler hth = new HttpTimeoutHandler(600000);
            URL conURL = new URL(null,url);
            conn = (HttpURLConnection) conURL.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            ins = conn.getInputStream();
            reader =new SAXReader();
            doc= reader.read(ins);
            //System.out.println(url);
            Element root=doc.getRootElement();
            String docXmlText=doc.asXML();
            //System.out.println(docXmlText);
            Element e=root.element("result");
            Element location=e.element("location");
            Element lng=location.element("lng");
            Element lat=location.element("lat");
            String lng1=lng.asXML();
            String lat1=lat.asXML();
lng1=lng1.substring(lng1.indexOf("<lng>")+5,lng1.indexOf("</lng>"));
             
            
            lat1=lat1.substring(lat1.indexOf("<lat>")+5,lat1.indexOf("</lat>"));
  
            System.out.println("lng:"+lng1);
            System.out.println("lat:"+lat1);
            location2=getLocation(lat1,lng1);
            List<Element> list = root.elements("location");
            //System.out.println(url);
            for (Element object : list) {
                //System.out.println(url);
                //System.out.println(object.getName());
                for (Element element : (List<Element>) object.elements()) {
                    //System.out.print(((Element) element).getName() + ":");
                    //System.out.print(element.getText() + " ");
                }
                //System.out.println();
 
            }
            ins.close();
            conn.disconnect();
           }catch (MalformedURLException e) {
            e.printStackTrace();
           } catch (IOException e) {
            e.printStackTrace();  
           } catch (DocumentException e) {
            e.printStackTrace();
           }catch(Exception e){
            e.printStackTrace();
           }finally {
            try {
             if (ins != null) {
              ins.close();
              ins = null;
             }
            } catch (IOException e1) {
             e1.printStackTrace();
            }
            try {
             if (conn != null) {
              conn.disconnect();
              conn = null;
             }
            } catch (Exception e2) {
             e2.printStackTrace();
            }
           }
           return location2;
        }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public static String getLocation(String lat,String lng){
         String location1="";
        String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=ld0uqubVfSTAUlXH5qIMN2F3Snsp16LU&output=xml&coordtype=wgs84ll&location="+lat+","+lng;
        //System.out.println(url);
        Document doc = null;
         java.net.HttpURLConnection conn = null;
         InputStream ins = null;
         SAXReader reader = null;
         try{
          //HttpTimeoutHandler hth = new HttpTimeoutHandler(600000);
          URL conURL = new URL(null,url);
          conn = (HttpURLConnection)conURL.openConnection();
          conn.setDoInput(true);
          conn.setDoOutput(true);
          conn.setUseCaches(false);
          ins = conn.getInputStream();
          reader =new SAXReader();
          doc= reader.read(ins);
          //System.out.println(url);
          Element root=doc.getRootElement();
          String docXmlText=doc.asXML();
          //System.out.println(docXmlText);
          Element e=root.element("result");
          Element location=e.element("formatted_address");
          location1=location.asXML();
          location1=location1.substring(location1.indexOf("address>")+8,location1.indexOf("</formatted_address>"));
         /* System.out.println("lng"+lng1);
          System.out.println("lat"+lat1);
         // System.out.println("location"+location.asXML());
          //System.out.println("xiayukun"+e.asXML());
          lng1=lng1.substring(lng1.indexOf("<lng>")+5,lng1.indexOf("</lng>"));
           
          System.out.println(lng1);
          lat1=lat1.substring(lat1.indexOf("<lat>")+5,lat1.indexOf("</lat>"));
          System.out.println(lat1);*/
          List<Element> list = root.elements("location");
          //System.out.println(url);
          for (Element object : list) {
              //System.out.println(url);
              //System.out.println(object.getName());
              for (Element element : (List<Element>) object.elements()) {
                  //System.out.print(((Element) element).getName() + ":");
                  //System.out.print(element.getText() + " ");
              }
              System.out.println();
 
          }
 
          ins.close();
          conn.disconnect();
         }catch (MalformedURLException e) {
          e.printStackTrace();
         } catch (IOException e) {
          e.printStackTrace();  
         } catch (DocumentException e) {
          e.printStackTrace();
         }catch(Exception e){
          e.printStackTrace();
         }finally {
          try {
           if (ins != null) {
            ins.close();
            ins = null;
           }
          } catch (IOException e1) {
           e1.printStackTrace();
          }
          try {
           if (conn != null) {
            conn.disconnect();
            conn = null;
           }
          } catch (Exception e2) {
           e2.printStackTrace();
          }
         }
         return location1;
      }
    public static void main(String[] args) throws IOException
    {
      System.out.println(addressResolution(getlocation1(addressResolution("河北省沧州市临港经济技术开发区规划一号路南"))));
    }

  截图:

posted on 2020-03-13 23:02  雨过山  阅读(105)  评论(0编辑  收藏  举报