parent
22eb0bbcc1
commit
831e3eeaaf
@ -0,0 +1,16 @@
|
||||
package com.mashibing.internalcommon.constant;
|
||||
|
||||
public class AmapConfigConstants {
|
||||
|
||||
public static final String DIRECTION_URL = "https://restapi.amap.com/v3/direction/driving";
|
||||
/*
|
||||
*路径规划 json key值
|
||||
*/
|
||||
public static final String STATUS = "status";
|
||||
public static final String ROUTE = "route";
|
||||
public static final String PATHS = "paths";
|
||||
|
||||
public static final String DISTANCE = "distance";
|
||||
public static final String DURATION = "duration";
|
||||
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
package com.mashibing.servicemap.remote;
|
||||
|
||||
import com.fasterxml.jackson.databind.util.JSONPObject;
|
||||
import com.mashibing.internalcommon.constant.AmapConfigConstants;
|
||||
import com.mashibing.internalcommon.response.DirectionResponse;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
public class MapDirectionClient {
|
||||
|
||||
@Value("${amap.key}")
|
||||
private String aMapKey;
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
public DirectionResponse direction(String depLongitude, String depLatitude, String destLongitude, String destLatitude){
|
||||
|
||||
// 组装 url
|
||||
// https://restapi.amap.com/v3/direction/driving?origin=116.481028,39.989643&destination=116.465302,40.004717&extensions=all&output=json&key=c5a197858bd6d73cf94cf49430fbe9d2
|
||||
StringBuilder urLBuild = new StringBuilder();
|
||||
urLBuild.append(AmapConfigConstants.DIRECTION_URL);
|
||||
urLBuild.append("?");
|
||||
urLBuild.append("origin="+depLongitude+","+depLatitude);
|
||||
urLBuild.append("&");
|
||||
urLBuild.append("destination="+destLongitude+","+destLatitude);
|
||||
urLBuild.append("&");
|
||||
urLBuild.append("extensions=base");
|
||||
urLBuild.append("&");
|
||||
urLBuild.append("output=json");
|
||||
urLBuild.append("&");
|
||||
urLBuild.append("key="+aMapKey);
|
||||
|
||||
// 调用高德接口
|
||||
ResponseEntity<String> forEntity = restTemplate.getForEntity(urLBuild.toString(), String.class);
|
||||
String directionStr = forEntity.getBody();
|
||||
|
||||
// 解析接口返回数据
|
||||
DirectionResponse directionResponse = parseDirectionInfo(directionStr);
|
||||
|
||||
return directionResponse;
|
||||
}
|
||||
|
||||
public DirectionResponse parseDirectionInfo(String directionStr){
|
||||
|
||||
DirectionResponse directionResponse = null;
|
||||
|
||||
JSONObject resultJObj = JSONObject.fromObject(directionStr);
|
||||
// 判断 是否可解析的状态值
|
||||
if (resultJObj.has(AmapConfigConstants.STATUS)) {
|
||||
Integer status = resultJObj.getInt(AmapConfigConstants.STATUS);
|
||||
if (status != null && status.intValue() == 1) {
|
||||
if (resultJObj.has(AmapConfigConstants.ROUTE)) {
|
||||
JSONObject routeJObj = resultJObj.getJSONObject(AmapConfigConstants.ROUTE);
|
||||
if (routeJObj != null && routeJObj.has(AmapConfigConstants.PATHS)) {
|
||||
|
||||
// 获取 所有行程路线的信息数组
|
||||
JSONArray pathJsonArray = routeJObj.getJSONArray(AmapConfigConstants.PATHS);
|
||||
if (pathJsonArray != null && pathJsonArray.size() > 0) {
|
||||
JSONObject pathArray1 = pathJsonArray.getJSONObject(0);
|
||||
|
||||
// 当 获取的第一条行程线路
|
||||
if (pathArray1 != null) {
|
||||
directionResponse = new DirectionResponse();
|
||||
if(pathArray1.has(AmapConfigConstants.DISTANCE)){
|
||||
Integer distance = pathArray1.getInt(AmapConfigConstants.DISTANCE);
|
||||
directionResponse.setDistance(distance);
|
||||
}
|
||||
if(pathArray1.has(AmapConfigConstants.DURATION)){
|
||||
Integer duration = pathArray1.getInt(AmapConfigConstants.DURATION);
|
||||
directionResponse.setDuration(duration);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return directionResponse;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue