生活随笔
收集整理的這篇文章主要介紹了
基于四维地图(四维图新)api进行逆地理编码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
基于四維地圖,輸入經(jīng)緯度坐標(biāo),獲得道路名稱,以及poi信息。
四維地圖api接口返回的數(shù)據(jù)有json和xml兩種格式。本篇是對xml數(shù)據(jù)進(jìn)行解析。
使用的是XStream對xml文件進(jìn)行解析。
返回的xml格式數(shù)據(jù)如下。
<xml><result><district_text>北京市>北京市>順義區(qū)</district_text><address>譽(yù)天下社區(qū)服務(wù)站西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>鄉(xiāng)鎮(zhèn)村道</road_level><lat>40.05941</lat><road_level_code>5</road_level_code></road><district>110113000</district><point><number>機(jī)場楊林出口1000米西北方向140米</number><lng>116.55255</lng><name>譽(yù)天下社區(qū)服務(wù)站</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我沒貼,測試的時候是得不到返回的數(shù)據(jù)的,因?yàn)檫@個uid是購買四維官方地圖服務(wù)之后提供的。
代碼如下:
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";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
.alias("xml", Params
.class);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
;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
){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
;}
}
運(yùn)行結(jié)果如下:
總結(jié)
以上是生活随笔為你收集整理的基于四维地图(四维图新)api进行逆地理编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。