liujie
2026-05-20 581646569a0273486c742a60961ecd98e60c6773
xp-robotaxi-maintenance-boot/src/main/java/com/xiaopeng/maintenance/utils/GDMapGeocodingUtil.java
@@ -1,19 +1,28 @@
package com.xiaopeng.maintenance.utils;
import com.alibaba.fastjson.JSON;
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.map.HistoryPointDto;
import com.xiaopeng.api.dto.operation.LonLatDto;
import com.xiaopeng.api.vo.operation.RouteVo;
import com.xiaopeng.common.core.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;
/**
 * 高德地图的地理编码工具类
 */
@Component
@Lazy
@Slf4j
public class GDMapGeocodingUtil {
    private String key = "66ae577f63898dae3016d599fda57221";
@@ -53,4 +62,93 @@
        return map;
    }
    /**
     * 根据起始点获取驾车距离 和 时间  以及避让区域
     *
     * @param start         起点
     * @param end           终点
     * @param avoidRoads    避让点
     * @param fenceId       mango id
     * @param routePlanRule 线路规划规则
     * @return
     * @throws Exception
     */
    public RouteVo getDistanceAndTime(LonLatDto start, LonLatDto end, ArrayList<JSONObject> avoidRoads, String fenceId, Integer routePlanRule, List<HistoryPointDto> history) {
        RouteVo routeVo = new RouteVo();
        String url = "https://tsapi.amap.com/v1/route/price?key=" + key + "&extensions=all&start=" + start.getLongitude() + "," + start.getLatitude();
        HashMap<String, Object> params = new HashMap<>();
        params.put("extensions", "all");
        params.put("start", start.getLongitude() + "," + start.getLatitude());
        params.put("end", end.getLongitude() + "," + end.getLatitude());
        params.put("routeUserType", 2);
        if (avoidRoads != null && !avoidRoads.isEmpty()) {
            params.put("avoidPolygons", avoidRoads);
        }
        if (routePlanRule != null) {
            params.put("strategy", routePlanRule);
        }
        if (null != history && !history.isEmpty()) {
            params.put("history", history);
        }
        log.info("高德地图接口:{}", url);
        log.info("高德地图参数:{}", JSON.toJSONString(params));
        String post = HttpUtils.post(url, JSON.toJSONString(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);
        log.info("先判断是否有大众常选的线路,优先选择大众常选的路线");
        for (Object o : path) {
            JSONObject jsonObject2 = (JSONObject) o;
            String routeID = jsonObject2.getString("routeID");
            String points = jsonObject2.getString("points");
            String label = jsonObject2.getString("label");
            if (label.contains("大众常选")) {
                String[] split = points.split(";");
                Integer costTime = jsonObject2.getInteger("costTime");
                Integer distance = jsonObject2.getInteger("distance");
                routeVo.setDistance(distance);
                routeVo.setDuration(costTime);
                ArrayList<LonLatDto> lonLatDtos = new ArrayList<>(split.length);
                for (String s : split) {
                    String[] split1 = s.split(",");
                    LonLatDto lonLatDto = new LonLatDto(Double.parseDouble(split1[0]), Double.parseDouble(split1[1]));
                    lonLatDtos.add(lonLatDto);
                }
                routeVo.setPolyline(lonLatDtos);
                routeVo.setRouteID(routeID);
                log.info("导航 {} 在 {} 运营区域内", routeVo.getRouteID(), fenceId);
                return routeVo;
            }
        }
        for (Object o : path) {
            JSONObject jsonObject2 = (JSONObject) o;
            String routeID = jsonObject2.getString("routeID");
            String points = jsonObject2.getString("points");
            String[] split = points.split(";");
            Integer costTime = jsonObject2.getInteger("costTime");
            Integer distance = jsonObject2.getInteger("distance");
            routeVo.setDistance(distance);
            routeVo.setDuration(costTime);
            ArrayList<LonLatDto> lonLatDtos = new ArrayList<>(split.length);
            for (String s : split) {
                String[] split1 = s.split(",");
                LonLatDto lonLatDto = new LonLatDto(Double.parseDouble(split1[0]), Double.parseDouble(split1[1]));
                lonLatDtos.add(lonLatDto);
            }
            routeVo.setPolyline(lonLatDtos);
            routeVo.setRouteID(routeID);
            log.info("导航 {} 在 {} 运营区域内", routeVo.getRouteID(), fenceId);
            return routeVo;
        }
        return null;
    }
}