题目要求:
基本完成地域维度标准化,将地址更新为省市县三级,并将行政区划代码添加。
效果图:
关键功能代码片段:
1 public static String addressResolution(String address){ 2 String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县|.+区|.+市)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)"; 3 Matcher m=Pattern.compile(regex).matcher(address); 4 String province=null,city=null,county=null; 5 String str=""; 6 while(m.find()){ 7 province=m.group("province"); 8 city=m.group("city"); 9 county=m.group("county"); 10 str = province + city + county; 11 System.out.println(str); 12 } 13 return str; 14 }
1 public static String getlocation1(String loc){ 2 String location2=""; 3 String url="http://api.map.baidu.com/geocoding/v3/?address="+loc+"10号&output=xml&ak=ld0uqubVfSTAUlXH5qIMN2F3Snsp16LU&callback=showLocation"; 4 System.out.println(url); 5 Document doc = null; 6 java.net.HttpURLConnection conn = null; 7 InputStream ins = null; 8 SAXReader reader = null; 9 try{ 10 //HttpTimeoutHandler hth = new HttpTimeoutHandler(600000); 11 URL conURL = new URL(null,url); 12 conn = (HttpURLConnection)conURL.openConnection(); 13 conn.setDoInput(true); 14 conn.setDoOutput(true); 15 conn.setUseCaches(false); 16 ins = conn.getInputStream(); 17 reader =new SAXReader(); 18 doc= reader.read(ins); 19 //System.out.println(url); 20 Element root=doc.getRootElement(); 21 String docXmlText=doc.asXML(); 22 //System.out.println(docXmlText); 23 Element e=root.element("result"); 24 Element location=e.element("location"); 25 Element lng=location.element("lng"); 26 Element lat=location.element("lat"); 27 String lng1=lng.asXML(); 28 String lat1=lat.asXML(); 29 System.out.println("lng"+lng1); 30 System.out.println("lat"+lat1); 31 // System.out.println("location"+location.asXML()); 32 //System.out.println("xiayukun"+e.asXML()); 33 lng1=lng1.substring(lng1.indexOf("<lng>")+5,lng1.indexOf("</lng>")); 34 35 System.out.println(lng1); 36 lat1=lat1.substring(lat1.indexOf("<lat>")+5,lat1.indexOf("</lat>")); 37 System.out.println(lat1); 38 location2=getLocation(lat1,lng1); 39 List<Element> list = root.elements("location"); 40 System.out.println(url); 41 for (Element object : list) { 42 System.out.println(url); 43 System.out.println(object.getName()); 44 for (Element element : (List<Element>) object.elements()) { 45 System.out.print(((Element) element).getName() + ":"); 46 System.out.print(element.getText() + " "); 47 } 48 System.out.println(); 49 50 } 51 52 ins.close(); 53 conn.disconnect(); 54 }catch (MalformedURLException e) { 55 e.printStackTrace(); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } catch (DocumentException e) { 59 e.printStackTrace(); 60 }catch(Exception e){ 61 e.printStackTrace(); 62 }finally { 63 try { 64 if (ins != null) { 65 ins.close(); 66 ins = null; 67 } 68 } catch (IOException e1) { 69 e1.printStackTrace(); 70 } 71 try { 72 if (conn != null) { 73 conn.disconnect(); 74 conn = null; 75 } 76 } catch (Exception e2) { 77 e2.printStackTrace(); 78 } 79 } 80 return location2; 81 }
部分运行结果截图: