基于四维地图(四维图新)api进行逆地理编码
程序员文章站
2022-07-06 08:27:51
基于四维地图,输入经纬度坐标,获得道路名称,以及poi信息。四维地图api接口返回的数据有json和xml两种格式。本篇是对xml数据进行解析。使用的是XStream对xml文件进行解析。返回的xml格式数据如下。 北京市>北京市>顺义区 誉天下社区服务站西163米
基于四维地图,输入经纬度坐标,获得道路名称,以及poi信息。
四维地图api接口返回的数据有json和xml两种格式。本篇是对xml数据进行解析。
使用的是XStream对xml文件进行解析。
返回的xml格式数据如下。
<xml>
<result>
<district_text>北京市>北京市>顺义区</district_text>
<address>誉天下社区服务站西163米</address>
<road>
<lng>116.55100</lng>
<distance>43</distance>
<lanenumber>一条车道</lanenumber>
<roadtype>无属性</roadtype>
<limit_speed>40.0</limit_speed>
<linkid>737249</linkid>
<urban>1</urban>
<name>花园六路</name>
<width>(5.5米~13米]</width>
<road_level>乡镇村道</road_level>
<lat>40.05941</lat>
<road_level_code>5</road_level_code>
</road>
<district>110113000</district>
<point>
<number>机场杨林出口1000米西北方向140米</number>
<lng>116.55255</lng>
<name>誉天下社区服务站</name>
<lat>40.05973</lat>
</point>
<road_address>花园六路西北43米</road_address>
</result>
<status>ok</status>
</xml>
所需要的依赖如下,不一定全。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
</dependency>
代码中有个APIKEY我没贴,测试的时候是得不到返回的数据的,因为这个uid是购买四维官方地图服务之后提供的。
代码如下:
package com.example.map2.map;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
public class SiweiMapXStream {
public static AtomicInteger num=new AtomicInteger(0);
public static String[] SERVERS=new String[]{
"a.map.icttic.cn",
"b.map.icttic.cn",
"c.map.icttic.cn",
};
public static void main(String[] args) {
String lng="116.54475678417967";
String lat="40.058573067126176";
reserveDecode(lng,lat);
}
public static String reserveDecode(String lng,String lat){
String location=lng+","+lat;
String APIKEY="xxxxx";//由于四维地图是付费服务,此处使用四维官方提供的uid即可,我原来测试使用的就不贴了,毕竟不是我买的
String server=getServer();
String url="http://"+server+":81/SE_RGC3?st=Rgc3&point="+location+"&uid="+APIKEY+"&output=xml&cd=wgs84&type=11";
System.out.println(url);
try {
String xmlString = doGet(url);
XStream xstream = new XStream(new DomDriver());
XStream.setupDefaultSecurity(xstream);
xstream.allowTypes(new Class[]{Params.class});//xStream对象设置默认安全防护,同时设置允许的类
xstream.alias("xml", Params.class);//此处"xml"对应xml文件中的根节点名称
Params params = (Params) xstream.fromXML(xmlString);
System.out.println(params.toString());
}catch (Exception e){
return "";
}
return null;
}
public static class Params{
public Result result;
public String status;
public String toString(){
try{
return result.equals(";;") ? "":result.toString();
}catch (Exception e){
return "";
}
}
}
private static class Result{
public String district_text;
public String address;
public String district;
public String road_address;
public Point point;
public Road road;
public String toString(){
try{
district_text=StringUtils.isBlank(district_text) ? "" : district_text.replaceAll(">","");
return district_text+";"+road.toString()+";"+point.toString();
}catch (Exception e){
return "";
}
}
}
private static class Road{
public String name;
public String name2;//四维地图api新增的一个字段,当输入特殊经纬度时会显示此字段,一般不显示
public String lng;
public String distance;
public String lanenumber;
public String roadtype;
public String limit_speed;
public String linkid;
public String urban;
public String width;
public String road_level;
public String lat;
public String road_level_code;
public String toString(){
try{
return StringUtils.isBlank(name)? (StringUtils.isBlank(name2)? "" : name2) : name;
}catch (Exception e){
return "";
}
}
}
private static class Point{
public String name;
public String number;
public String lng;
public String lat;
public Point(){}
public String toString(){
try{
return StringUtils.isBlank(name) ? "" : name;
}catch (Exception e){
return "";
}
}
}
public static String getServer(){
num.addAndGet(1);
int i=num.get()%SERVERS.length;
return SERVERS[i];
}
public static String doGet(String url){
//创建http客户端
CloseableHttpClient client= HttpClientBuilder.create().build();
//请求方式
HttpGet httpGet1=new HttpGet(url);
//创建响应实体
CloseableHttpResponse response1=null;
try {
response1= client.execute(httpGet1);
HttpEntity responseEntity=response1.getEntity();
if(responseEntity != null){
return EntityUtils.toString(responseEntity);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(client != null){
client.close();
}
if (response1 != null){
response1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
运行结果如下:
本文地址:https://blog.csdn.net/qq_37786081/article/details/107367455