第四周学习记录
通过调用相关的接口,实现输入“名字”(公司名、公园等)输出“所在区域”(省市区)。
实现:
package Dao; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.sql.SQLException; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import net.sf.json.JSONNull; import net.sf.json.JSONObject; import Dao.LoadDaoImpl; public class getDiyu{ public String[] getPosition_name_code(String infos) throws ClassNotFoundException, SQLException{//获取行政区划 LoadDaoImpl load = new LoadDaoImpl(); String moreinfo[] = new String[2]; String[] company = infos.split(" "); String info = company[0];//第一单位获取地域 String position = getLocation(info); String first = ""; @SuppressWarnings("unused") String second = ""; if(position.substring(0,2).equals("北京")||position.substring(0,2).equals("天津")) { String[] dizhi = position.split("区"); String[] shixian = dizhi[0].split("市"); second = shixian[1]+"区";//获取县级 first = dizhi[0]+"区";//获取市区 }else if(position.substring(0,2).equals("河北")) {//获取省市县 if(position.contains("区")) { String[] dizhi = position.split("区"); String[] shixian = dizhi[0].split("市"); second = shixian[1]+"区"; first = dizhi[0]+"区"; }else if(position.contains("县")) { String[] dizhi = position.split("县"); String[] shixian = dizhi[0].split("市"); second = shixian[1]+"县"; first = dizhi[0]+"县"; } }else { position = "未找到位置信息。"; first = "未找到位置信息。"; } moreinfo[0]=first; moreinfo[1]=String.valueOf(load.loadcode(second)); if(moreinfo[1].equals("0")) { moreinfo[1]="未找到位置信息。"; } if(moreinfo[0].equals("")) { moreinfo[0]="未找到位置信息。"; } return moreinfo; } @SuppressWarnings("unchecked") //获得经纬度 public static String getPosition(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); Element root=doc.getRootElement(); 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>")); 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 String getLngLat(String address) { StringBuffer json = new StringBuffer(); try { URL u = new URL("http://restapi.amap.com/v3/geocode/geo?address="+address+"&output=JSON&key=7f4ffae4074e8b8e4d147190527a4b72"); URLConnection yc = u.openConnection(); //读取返回的数据 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8")); String inputline = null; while((inputline=in.readLine())!=null){ json.append(inputline); } in.close(); } catch (Exception e) { e.printStackTrace(); } String jsonStr=json.toString(); JSONObject jsonObject = JSONObject.fromObject(jsonStr); //判断输入的位置点是否存在 /*jsonObject.getJSONArray("geocodes").size()>0*/ if(!JSONNull.getInstance().equals(jsonObject.get("geocode"))) return jsonObject.getJSONArray("geocode").getJSONObject(0).get("location").toString(); else return null; } @SuppressWarnings("unchecked") //由获取的经纬度得到区域名字 public static String getLocation(String loc){ String location2=""; String url="http://api.map.baidu.com/geocoding/v3/?address="+loc+"10号&output=xml&ak=ld0uqubVfSTAUlXH5qIMN2F3Snsp16LU&callback=showLocation"; Document doc = null; java.net.HttpURLConnection conn = null; InputStream ins = null; SAXReader reader = null; try{ 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); Element root=doc.getRootElement(); Element e=root.element("result"); System.out.println(loc); 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>")); location2=getPosition(lat1,lng1); List<Element> list = root.elements("location"); for (Element object : list) { //System.out.println(object.getName()); for (Element element : (List<Element>) object.elements()) { //System.out.print(((Element) element).getName() + ":"); //System.out.print(element.getText() + " "); } } ins.close(); conn.disconnect(); }catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { return "未查找到位置信息。"; }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[] getRegion(String company) throws ClassNotFoundException, SQLException { getDiyu pos = new getDiyu(); String moreinfo[] = new String[2]; moreinfo = pos.getPosition_name_code(company); return moreinfo; } public static void main(String[] strgs) throws ClassNotFoundException, SQLException{ getRegion("石家庄铁道大学"); } }
浙公网安备 33010602011771号