进度日报(二)

今天可以提取关键字,输出详细的省市县(区),并调用百度地图接口输出准确的经纬度,并输出所在的市县。

关键代码:

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

  

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

  

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 @ 2020-03-13 20:26  Double晨  阅读(245)  评论(0编辑  收藏  举报