puzhibing
2026-03-26 5f7a82bca016b27f8e05d2892ca7b64b0d7d9797
xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/utils/GDMapGeocodingUtil.java
@@ -1,8 +1,8 @@
package com.xiaopeng.map.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xiaopeng.api.dto.operation.LonLatDto;
import com.xiaopeng.api.vo.operation.RouteVo;
import com.xiaopeng.common.core.utils.HttpUtils;
@@ -14,7 +14,10 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * 高德地图的地理编码工具类
@@ -29,6 +32,7 @@
    @Autowired
    private XpGeoFencingPolygonService xpGeoFencingPolygonService;
    /**
@@ -58,32 +62,33 @@
        if(routePlanRule!=null){
            params.put("strategy", routePlanRule);
        }
        log.info("高德地图接口:{}", url);
        log.info("高德地图参数:{}", params);
        String post = HttpUtils.post(url, JSON.toJSONString(params));
        System.out.println("高德地图返回结果:"+ post);
//        String forObject = restTemplate.getForObject(url, String.class, params);
        log.info("高德地图返回结果:"+ post);
        JSONObject jsonObject = JSON.parseObject(post);
        String data = jsonObject.getString("data");
        JSONObject jsonObject1 = JSON.parseObject(data);
        JSONArray path = jsonObject1.getJSONArray("paths");
        log.info("开始判断导航路径是否在运营区域范围内 {}", fenceId);
        for (Object o : path) {
            JSONObject jsonObject2 = (JSONObject) o;
            String routeID = jsonObject2.getString("routeID");
            String points = jsonObject2.getString("points");
            String[] split = points.split(";");
            int temp=0;
            // 取5分之一的点位
            List<LonLatDto> pointsArray = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                if (i % 5 == 0) {
                if((i % 2) == 0){
                    String[] split1 = split[i].split(",");
                    LonLatDto lonLatDto = new LonLatDto(Double.parseDouble(split1[0]), Double.parseDouble(split1[1]));
                    if (!xpGeoFencingPolygonService.isInFence(lonLatDto.getLongitude(), lonLatDto.getLatitude(), fenceId, "operating_area")) {
                        temp++;
                        break;
                    }
                    pointsArray.add(lonLatDto);
                }
            }
            ForkJoinPool pool = new ForkJoinPool(); // 创建ForkJoinPool对象(默认使用处理器的核心数作为线程池大小)
            ArrayPointsTask arrayPointsTask = new ArrayPointsTask(pointsArray, fenceId); // 创建求和任务对象并传入数组作为参数(这里也可以传入数组的一部分作为参数来实现更细粒度的拆分)
            Long temp = pool.invoke(arrayPointsTask); // 提交任务并等待处理完成(也可以使用submit方法提交任务并获取一个Future对象来异步获取结果)
            pool.shutdown(); // 关闭ForkJoinPool(释放资源)
            if(temp==0){
                Integer costTime = jsonObject2.getInteger("costTime");
                Integer distance = jsonObject2.getInteger("distance");
@@ -97,15 +102,70 @@
                }
                routeVo.setPolyline(lonLatDtos);
                routeVo.setRouteID(routeID);
                log.info("导航 {} 在 {} 运营区域内", routeVo.getRouteID(), fenceId);
                return routeVo;
            }
        }
        return null;
    }
    public static void main(String[] args) {
        String url = "https://tsapi.amap.com/v1/route/price?key=" + "66ae577f63898dae3016d599fda57221" + "&extensions=all&start=" +"113.384932" + "," + "23.155455";
        HashMap<String, Object> params = new HashMap<>();
        params.put("extensions", "all");
        params.put("start", "113.384932" + "," + "23.155455");
        params.put("end", "113.388209" + "," + "23.163455");
        params.put("routeUserType",2);
        log.info("高德地图接口:{}", url);
        log.info("高德地图参数:{}", params);
        String post = HttpUtils.post(url, JSON.toJSONString(params));
        log.info("高德地图返回结果:"+ post);
        JSONObject jsonObject = JSON.parseObject(post);
        System.out.println(jsonObject);
    }
    class ArrayPointsTask extends RecursiveTask<Long>{
        //最大定位集合阈值
        private final int THRESHOLD = 10;
        //围栏ID
        String fenceId;
        //定位点集合
        List<LonLatDto> pointsArray;
        public ArrayPointsTask(List<LonLatDto> pointsArray, String fenceId) {
            this.pointsArray = pointsArray;
            this.fenceId = fenceId;
        }
        @Override
        protected Long compute() {
            if(this.pointsArray.size() > THRESHOLD){
                int num = this.pointsArray.size() / 2;
                ArrayPointsTask task1 = new ArrayPointsTask(this.pointsArray.subList(0, num), fenceId);
                ArrayPointsTask task2 = new ArrayPointsTask(this.pointsArray.subList(num, this.pointsArray.size()), fenceId);
                task1.fork();
                task2.fork();
                return task1.join() + task2.join();
            }else{
                long temp = 0;
                for (LonLatDto lonLatDto : this.pointsArray) {
                    if (!xpGeoFencingPolygonService.isInFence(lonLatDto.getLongitude(), lonLatDto.getLatitude(), fenceId, "operating_area")) {
                        temp++;
                        break;
                    }
                }
                return temp;
            }
        }
    }
    /**
     * 线路还原接口
     * 车端使用的数据