|
@@ -0,0 +1,54 @@
|
|
|
+package com.szwl.util;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.sun.org.apache.xpath.internal.SourceTree;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+
|
|
|
+public class geoCoderUtil {
|
|
|
+ // 地理位置逆编码
|
|
|
+ private static final String TD_KEY = "26bac63d19fac57c16bc7759455431d8";
|
|
|
+
|
|
|
+ private static final String TD_URL = "http://api.tianditu.gov.cn/geocoder";
|
|
|
+ // 请求:http://api.tianditu.gov.cn/geocoder?postStr={'lon':116.37304,'lat':39.92594,'ver':1}&type=geocode&tk=您的密钥
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static String getLocByLonLatVer(String lon, String lat) throws Exception {
|
|
|
+ String ver = "1"; // 版本号
|
|
|
+ String reqUrl = TD_URL + "?postStr={'lon':" + lon + ",'lat':" + lat + ",'ver':" + ver + "}&type=geocode&tk=" + URLEncoder.encode(TD_KEY, "UTF-8");
|
|
|
+ String s = HttpUtil.get(reqUrl);
|
|
|
+ return getNotionAddr(s);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getNotionAddr(String result) {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+
|
|
|
+ try {
|
|
|
+ JsonNode node = mapper.readTree(result);
|
|
|
+
|
|
|
+ String nation = node.at("/result/addressComponent/nation").asText();
|
|
|
+ String address = node.at("/result/addressComponent/address").asText();
|
|
|
+
|
|
|
+// System.out.println("Nation + Address:" + nation + address);
|
|
|
+ return nation + address;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void main(String[] args) throws Exception {
|
|
|
+// String lon = "127.0013";
|
|
|
+// String lat = "37.5015";
|
|
|
+//
|
|
|
+// String response = getLocByLonLatVer(lon, lat);
|
|
|
+// System.out.println(response);
|
|
|
+// }
|
|
|
+}
|
|
|
+
|