From 9e3ecfab0113a6b45e30e0ac64e51e8df53c4f72 Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期二, 10 三月 2026 21:07:57 +0800
Subject: [PATCH] 修改bug

---
 xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java |   39 +++++++++++++++++++++++++++++++++++++--
 1 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java b/xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java
index 5f49af6..b25bab7 100644
--- a/xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java
+++ b/xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/service/serviceimpl/XpElectronicFenceServiceImpl.java
@@ -31,6 +31,10 @@
 import com.xiaopeng.map.utils.PolygonOverlapService;
 import lombok.extern.slf4j.Slf4j;
 import net.sourceforge.pinyin4j.PinyinHelper;
+import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.GeometryFactory;
+import org.locationtech.jts.geom.LinearRing;
+import org.locationtech.jts.geom.Polygon;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.geo.Point;
 import org.springframework.data.mongodb.core.MongoTemplate;
@@ -40,6 +44,7 @@
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.awt.geom.Point2D;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.List;
@@ -166,7 +171,13 @@
         //根据经纬度获取当前城市code
         XpElectronicFenceArea xpElectronicFenceArea = dto.getElectronicFenceAreas().get(0);
         String[] split = xpElectronicFenceArea.getFencePoints().split(",")[0].split("_");
-        Map<String, String> geocode = gdMapGeocodingUtil.geocode(split[0], split[1]);
+        Coordinate[] coordinates = new Coordinate[split.length];
+        for (int i = 0; i < split.length; i++) {
+            String[] split1 = split[i].split("_");
+            coordinates[i] = new Coordinate(Double.parseDouble(split1[0]), Double.parseDouble(split1[1]));
+        }
+        org.locationtech.jts.geom.Point point = calculateCentroid(coordinates);
+        Map<String, String> geocode = gdMapGeocodingUtil.geocode(point.getX() + "", point.getY() + "");
         if(null == geocode){
             return R.fail("经纬度转换城市code失败");
         }
@@ -383,7 +394,13 @@
             //根据经纬度获取当前城市code
             XpElectronicFenceArea electronicFenceArea = dto.getElectronicFenceAreas().get(0);
             String[] split = electronicFenceArea.getFencePoints().split(",")[0].split("_");
-            Map<String, String> geocode = gdMapGeocodingUtil.geocode(split[0], split[1]);
+            Coordinate[] coordinates = new Coordinate[split.length];
+            for (int i = 0; i < split.length; i++) {
+                String[] split1 = split[i].split("_");
+                coordinates[i] = new Coordinate(Double.parseDouble(split1[0]), Double.parseDouble(split1[1]));
+            }
+            org.locationtech.jts.geom.Point point = calculateCentroid(coordinates);
+            Map<String, String> geocode = gdMapGeocodingUtil.geocode(point.getX() + "", point.getY() + "");
             if(null == geocode){
                 return R.fail("经纬度转换城市code失败");
             }
@@ -696,4 +713,22 @@
     }
 
 
+    /**
+     * 计算围栏中心点
+     * @param coordinates
+     * @return
+     */
+    private org.locationtech.jts.geom.Point calculateCentroid(Coordinate[] coordinates) {
+        GeometryFactory factory = new GeometryFactory();
+        // 创建线性环 (LinearRing)
+        LinearRing ring = factory.createLinearRing(coordinates);
+        // 创建多边形 (Polygon)
+        Polygon polygon = factory.createPolygon(ring);
+        // 计算质心 (Centroid)
+        org.locationtech.jts.geom.Point centroid = polygon.getCentroid();
+
+        System.out.println("中心点经度: " + centroid.getX());
+        System.out.println("中心点纬度: " + centroid.getY());
+        return centroid;
+    }
 }

--
Gitblit v1.7.1