第二周周总结
2020.02.29
本周主要内容:通过项目河北创新项目年报平台系统,其需要同一规范填报单位所在行政区划,解:通过调用百度地图API,来获取已知地域的所属行政区,例如 北京市环境卫生设计科学研究所 其所定位为:北京市朝阳区
其思路为首先通过百度api来获取该地域的经度和纬度,再根据经度纬度来识别其所在的行政区划
public static String getDiyu(String lat,String lng){//已知经纬度,传参给该方法通过调用百度API识别出所属行政区
String location1="";
String url="http://api.map.baidu.com/reverse_geocoding/v3/?ak=ld0uqubVfSTAUlXH5qIMN2F3Snsp16LU&output=xml&coordtype=wgs84ll&location="+lat+","+lng;
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();
String docXmlText=doc.asXML();
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");
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 String getDiyu1(String loc){ //通过百度API获取该地域的经纬度
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();
String docXmlText=doc.asXML();
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>"));
location2=getDiyu(lat1,lng1);
List<Element> list = root.elements("location");
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;
}

浙公网安备 33010602011771号