From 1ebda3975a267f73e0d00f577ab5c18d17931309 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期二, 14 一月 2025 12:02:37 +0800
Subject: [PATCH] 补充

---
 DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java |   51 +++++++++++++++++++++++++++++++++++----------------
 1 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java b/DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
index 01cf418..f181e48 100644
--- a/DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
+++ b/DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
@@ -20,7 +20,7 @@
 @Component
 public class GDMapGeocodingUtil {
 
-    private String key = "3491db7ed190e5e4757fcb838e012130";
+    private String key = "WQhfsluNzEeUHUxoH4jc4JiCQOXw4Mnx";
 
     @Autowired
     private RestTemplate restTemplate;
@@ -31,25 +31,26 @@
 
     /**
      * 将行政区域名称转化为坐标
+     *
      * @param province
      * @param city
      * @param county
      * @param address
      * @return
      */
-    public Map<String, Object> geocoding(String province, String city, String county, String address){
+    public Map<String, Object> geocoding(String province, String city, String county, String address) {
         Map<String, Object> map = new HashMap<>();
-        if(ToolUtil.isEmpty(province)){
+        if (ToolUtil.isEmpty(province)) {
             map.put("status", -1);
             map.put("data", "省不能为空");
             return map;
         }
-        if((ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(county)) || (ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(address))){
+        if ((ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(county)) || (ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(address))) {
             map.put("status", -1);
             map.put("data", "市不能为空");
             return map;
         }
-        if((ToolUtil.isEmpty(county) && ToolUtil.isNotEmpty(address))){
+        if ((ToolUtil.isEmpty(county) && ToolUtil.isNotEmpty(address))) {
             map.put("status", -1);
             map.put("data", "县/区不能为空");
             return map;
@@ -64,9 +65,9 @@
 
 //        gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/geo", "行政区域转经纬度");
 
-        if(status.equals("1")){
+        if (status.equals("1")) {
             JSONArray geocodes = jsonObject.getJSONArray("geocodes");
-            for(int i = 0; i < geocodes.size(); i++){
+            for (int i = 0; i < geocodes.size(); i++) {
                 String location = geocodes.getJSONObject(i).getString("location");
                 list.add(location);
             }
@@ -77,25 +78,42 @@
     }
 
 
+    public Map<String, Object> geocoding(String address) {
+        Map<String, Object> map = new HashMap<>();
+        String url = "https://api.map.baidu.com/geocoding/v3/?address"+ address+"&output=json&ak=" +key+"&ret_coordtype="+"bd09ll";
+        String forObject = restTemplate.getForObject(url, String.class);
+        JSONObject jsonObject = JSON.parseObject(forObject);
+        String status = jsonObject.getString("status");
+        List<String> list = new ArrayList<>();
+        if (status.equals("0")) {
+            JSONObject result = jsonObject.getJSONObject("result");
+            JSONObject location = result.getJSONObject("location");
+            String lng = location.getString("lng");
+            String lat = location.getString("lat");
+            list.add(lat + "," + lng);
+        }
+        map.put("status", 0);
+        map.put("data", list);
+        return map;
+    }
+
     /**
      * 根据经纬度获取行政区域信息
+     *
      * @param lon
      * @param lan
      * @return
      * @throws Exception
      */
-    public Map<String, String> geocode(String lon, String lan) throws Exception{
-        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan;
+    public Map<String, String> geocode(String lon, String lan) throws Exception {
+        String url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak=" + key + "&output=json&coordtype=bd09ll" + "&location=" + lan + "," + lon;
         String forObject = restTemplate.getForObject(url, String.class);
         JSONObject jsonObject = JSON.parseObject(forObject);
         Map<String, String> map = new HashMap<>();
-
-//        gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/regeo", "经纬度转行政区域");
-
-        if(jsonObject.getString("status").equals("1")){
-            JSONObject regeocode = jsonObject.getJSONObject("regeocode");
-            JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
-            String address = regeocode.getString("formatted_address");
+        if (jsonObject.getString("status").equals("0")) {
+            JSONObject result = jsonObject.getJSONObject("result");
+            JSONObject addressComponent = result.getJSONObject("addressComponent");
+            String address = result.getString("formatted_address");
             map.put("address", address);
             String code = addressComponent.getString("adcode");
             String province = addressComponent.getString("province");
@@ -111,3 +129,4 @@
         return map;
     }
 }
+

--
Gitblit v1.7.1