puzhibing
2026-02-04 85bafa86f44fbe6646019fd8ecbab79f552ccd0a
xp-service/xp-map/src/main/java/com/xiaopeng/map/utils/GDMapGeocodingUtil.java
File was renamed from xp-service/xp-operation/src/main/java/com/xiaopeng/operation/utils/map/GDMapGeocodingUtil.java
@@ -1,8 +1,11 @@
package com.xiaopeng.operation.utils.map;
package com.xiaopeng.map.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaopeng.api.dto.operation.LonLatDto;
import com.xiaopeng.api.vo.operation.RouteVo;
import com.xiaopeng.map.utils.mangoUtils.MongoGeoFenceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@@ -23,6 +26,9 @@
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private MongoGeoFenceUtil mongoGeoFenceUtil;
    /**
@@ -125,4 +131,69 @@
        return map;
    }
    /**
     * 根据起始点获取驾车距离 和 时间  以及避让区域
     *
     * @return
     * @throws Exception
     */
    public RouteVo getDistanceAndTime(LonLatDto start, LonLatDto end,List<LonLatDto> avoidRoads,String fenceId)  {
        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);
        params.put("voidPolygons", JSON.toJSONString(avoidRoads));
        String forObject = restTemplate.getForObject(url, String.class, params);
        JSONObject jsonObject = JSON.parseObject(forObject);
        String data = jsonObject.getString("data");
        JSONObject jsonObject1 = JSON.parseObject(data);
        String routeID = jsonObject1.getString("routeID");
        JSONArray path = jsonObject1.getJSONArray("path");
        for (Object o : path) {
            JSONObject jsonObject2 = (JSONObject) o;
            String points = jsonObject2.getString("points");
            String[] split = points.split(";");
            int temp=0;
            // 取5分之一的点位
            for (int i = 0; i < split.length; i++) {
                if (i % 5 == 0) {
                    String[] split1 = split[i].split(",");
                    LonLatDto lonLatDto = new LonLatDto(Double.parseDouble(split1[0]), Double.parseDouble(split1[1]));
                    if (!mongoGeoFenceUtil.isInFence(lonLatDto.getLongitude(), lonLatDto.getLatitude(), fenceId, "operating_area")) {
                        temp++;
                        break;
                    }
                }
            }
            if(temp==0){
                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);
                return routeVo;
            }
        }
        return null;
    }
}