liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/utils/AmapWalkingRouteUtil.java
@@ -8,12 +8,13 @@
import com.xiaopeng.api.vo.map.PointByIpVo;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.HttpUtils;
import com.xiaopeng.map.config.MapApiConfigHolder;
import com.xiaopeng.map.config.MapApiProperties;
import com.xiaopeng.map.constant.I18nKeyConstant;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashMap;
@@ -26,29 +27,20 @@
@Slf4j
public class AmapWalkingRouteUtil {
    // 高德地图Web服务API配置
    private static final String AMAP_API_BASE_URL = "https://restapi.amap.com/v3/direction/walking";
    private static final String AMAP_GEOCODING_URL = "https://restapi.amap.com/v3/geocode/geo";
    // 高德API基础URL
    private static final String IP_API_URL = "https://restapi.amap.com/v3/ip";
    private static final String GEOCODE_API_URL = "https://restapi.amap.com/v3/geocode/geo";
    // HTTP客户端
    private final OkHttpClient httpClient;
    private final ObjectMapper objectMapper;
    //    @Value("${map.apikey}")
    private static String apiKey="66ae577f63898dae3016d599fda57221";
    private static String apiKey;
    /**
     * IP定位
     */
    public static PointByIpVo ipLocation(String ip) throws IOException {
        PointByIpVo pointByIpVo = new PointByIpVo();
        StringBuilder urlBuilder = new StringBuilder(IP_API_URL)
                .append("?key=").append(apiKey)
        StringBuilder urlBuilder = new StringBuilder(getMapProperties().getUrls().getIp())
                .append("?key=").append(getApiKey())
                .append("&output=json");
        if (ip != null && !ip.trim().isEmpty()) {
@@ -99,7 +91,7 @@
    }
    private static LonLatDto geocodeCity(String cityName) throws IOException {
        String url = GEOCODE_API_URL + "?key=" +apiKey + "&address=" + cityName + "&output=json&city=" + cityName;
        String url = getMapProperties().getUrls().getGeocode() + "?key=" + getApiKey() + "&address=" + cityName + "&output=json&city=" + cityName;
        String response = HttpUtils.sendGet(url);
        System.out.println(response);
        JSONObject jsonObject = JSONObject.parseObject(response);
@@ -120,18 +112,12 @@
        return new LonLatDto(longitude, latitude);
    }
        public static void main (String[]args) throws IOException {
            geocodeCity("凉山彝族自治州");
        }
        /**
         * 构造函数
         * @param apiKey 高德地图API Key
         */
    public AmapWalkingRouteUtil(String apiKey) {
            this.apiKey = apiKey;
            AmapWalkingRouteUtil.apiKey = apiKey;
            this.httpClient = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
@@ -259,10 +245,10 @@
         */
        public Location getLocationByAddress (String address) throws IOException {
            Map<String, String> params = new HashMap<>();
            params.put("key", apiKey);
            params.put("key", getApiKey());
            params.put("address", address);
            String url = buildUrl(AMAP_GEOCODING_URL, params);
            String url = buildUrl(getMapProperties().getUrls().getGeocode(), params);
            Request request = new Request.Builder().url(url).build();
            try (Response response = httpClient.newCall(request).execute()) {
@@ -299,11 +285,11 @@
            try {
                Map<String, String> params = new HashMap<>();
                params.put("key", apiKey);
                params.put("key", getApiKey());
                params.put("origin", origin.toString());
                params.put("destination", destination.toString());
                String url = buildUrl(AMAP_API_BASE_URL, params);
                String url = buildUrl(getMapProperties().getUrls().getWalking(), params);
                Request request = new Request.Builder().url(url).build();
                try (Response response = httpClient.newCall(request).execute()) {
@@ -368,10 +354,10 @@
        private String getAddressByLocation (Location location){
            try {
                Map<String, String> params = new HashMap<>();
                params.put("key", apiKey);
                params.put("key", getApiKey());
                params.put("location", location.toString());
                String url = buildUrl("https://restapi.amap.com/v3/geocode/regeo", params);
                String url = buildUrl(getMapProperties().getUrls().getReverseGeocode(), params);
                Request request = new Request.Builder().url(url).build();
                try (Response response = httpClient.newCall(request).execute()) {
@@ -430,5 +416,16 @@
            return earthRadius * c;
        }
        private static MapApiProperties getMapProperties() {
            return MapApiConfigHolder.getProperties();
        }
    }
        private static String getApiKey() {
            if (apiKey != null) {
                return apiKey;
            }
            return getMapProperties().getApiKey();
        }
    }