| | |
| | | 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; |
| | |
| | | @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()) { |
| | |
| | | } |
| | | |
| | | 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); |
| | |
| | | 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) |
| | |
| | | */ |
| | | 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()) { |
| | |
| | | |
| | | 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()) { |
| | |
| | | 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()) { |
| | |
| | | return earthRadius * c; |
| | | } |
| | | |
| | | private static MapApiProperties getMapProperties() { |
| | | return MapApiConfigHolder.getProperties(); |
| | | } |
| | | |
| | | } |
| | | private static String getApiKey() { |
| | | if (apiKey != null) { |
| | | return apiKey; |
| | | } |
| | | return getMapProperties().getApiKey(); |
| | | } |
| | | |
| | | |
| | | } |