|
@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.HashMap;
|
|
@@ -51,4 +52,51 @@ public class BaiduApiUtil {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据项目位置获取经纬度
|
|
|
+ */
|
|
|
+ public static Map<String, Object> geocoding(String address)
|
|
|
+ throws IOException {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ BufferedReader in = null;
|
|
|
+ String getUrl = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + API_AK;
|
|
|
+ URL url = new URL(getUrl);
|
|
|
+ in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
|
|
|
+ String res;
|
|
|
+ StringBuilder sb = new StringBuilder("");
|
|
|
+ while ((res = in.readLine()) != null) {
|
|
|
+ sb.append(res.trim());
|
|
|
+ }
|
|
|
+ JSONObject jsonData = JSONObject.parseObject(sb.toString());
|
|
|
+ JSONObject result = (JSONObject) jsonData.get("result");
|
|
|
+ JSONObject location = (JSONObject) result.get("location");
|
|
|
+ map.put("lat", location.get("lat").toString());
|
|
|
+ map.put("lng", location.get("lng").toString());
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据经纬度坐标解析地址详情
|
|
|
+ */
|
|
|
+ public static Map<String, String> getPosition(String LatitudeAndLongitude)
|
|
|
+ throws IOException {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ String getUrl = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=" + API_AK + "&output=json&coordtype=wgs84ll&location=" + LatitudeAndLongitude;
|
|
|
+ URL url = new URL(getUrl);
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
|
|
|
+ String res;
|
|
|
+ StringBuilder sb = new StringBuilder("");
|
|
|
+ while ((res = in.readLine()) != null) {
|
|
|
+ sb.append(res.trim());
|
|
|
+ }
|
|
|
+ JSONObject jsonData = JSONObject.parseObject(sb.toString());
|
|
|
+ JSONObject result = (JSONObject) jsonData.get("result");
|
|
|
+ JSONObject addressComponent = (JSONObject) result.get("addressComponent");
|
|
|
+ map.put("province", addressComponent.get("province").toString());
|
|
|
+ map.put("city", addressComponent.get("city").toString());
|
|
|
+ map.put("district", addressComponent.get("district").toString());
|
|
|
+ map.put("adcode", addressComponent.get("adcode").toString());
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
}
|