package com.xiaopeng.map.utils;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.xiaopeng.map.config.MapApiConfigHolder;
|
import com.xiaopeng.map.config.MapApiProperties;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.util.EntityUtils;
|
|
import java.net.URLEncoder;
|
import java.nio.charset.StandardCharsets;
|
|
public class GaodeMapService {
|
|
private final String walkingApiUrl;
|
|
private final String apiKey;
|
|
public GaodeMapService() {
|
MapApiProperties properties = MapApiConfigHolder.getProperties();
|
this.walkingApiUrl = properties.getUrls().getWalking();
|
this.apiKey = properties.getApiKey();
|
}
|
|
/**
|
* 获取步行路径规划
|
* @param origin 起点坐标,格式:经度,纬度
|
* @param destination 终点坐标,格式:经度,纬度
|
* @return 步行路线结果
|
*/
|
public AmapWalkingRouteUtil.WalkingRouteResult getWalkingRoute(String origin, String destination) {
|
AmapWalkingRouteUtil.WalkingRouteResult result = new AmapWalkingRouteUtil.WalkingRouteResult();
|
result.setOrigin(origin);
|
result.setDestination(destination);
|
|
try {
|
// 构建请求URL
|
String url = buildWalkingUrl(origin, destination);
|
|
// 发送HTTP请求
|
String responseJson = sendHttpGetRequest(url);
|
|
// 解析响应
|
return parseWalkingResponse(responseJson, result);
|
|
} catch (Exception e) {
|
result.setSuccess(false);
|
return result;
|
}
|
}
|
|
/**
|
* 构建步行路径规划URL
|
*/
|
private String buildWalkingUrl(String origin, String destination) {
|
return String.format("%s?origin=%s&destination=%s&key=%s",
|
walkingApiUrl,
|
origin,
|
destination,
|
apiKey);
|
}
|
|
/**
|
* 发送HTTP GET请求
|
*/
|
private String sendHttpGetRequest(String url) throws Exception {
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
HttpGet httpGet = new HttpGet(url);
|
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
HttpEntity entity = response.getEntity();
|
if (entity != null) {
|
return EntityUtils.toString(entity, StandardCharsets.UTF_8);
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 解析步行响应结果
|
*/
|
private AmapWalkingRouteUtil.WalkingRouteResult parseWalkingResponse(String responseJson, AmapWalkingRouteUtil.WalkingRouteResult result) throws Exception {
|
ObjectMapper objectMapper = new ObjectMapper();
|
JsonNode rootNode = objectMapper.readTree(responseJson);
|
|
// 检查状态码
|
String status = rootNode.path("status").asText();
|
if (!"1".equals(status)) {
|
result.setSuccess(false);
|
return result;
|
}
|
|
// 解析路径信息
|
JsonNode routeNode = rootNode.path("route");
|
JsonNode pathsNode = routeNode.path("paths");
|
|
if (pathsNode.isArray() && pathsNode.size() > 0) {
|
JsonNode firstPath = pathsNode.get(0);
|
|
// 获取距离和时间
|
int distance = firstPath.path("distance").asInt();
|
int duration = firstPath.path("duration").asInt();
|
|
result.setDistance(distance);
|
result.setDuration(duration);
|
result.setSuccess(true);
|
} else {
|
result.setSuccess(false);
|
}
|
|
return result;
|
}
|
}
|