飞滴出行网约车2022-service-map调用高德地图获取路径信息

master
yh 3 years ago
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";
}

@ -2,10 +2,19 @@ package com.mashibing.servicemap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ServiceMapApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceMapApplication.class);
}
@Bean
public RestTemplate restTemplate ( ) {
return new RestTemplate();
}
}

@ -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;
}
}

@ -2,13 +2,18 @@ package com.mashibing.servicemap.service;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.DirectionResponse;
import com.mashibing.servicemap.remote.MapDirectionClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class DirectionService {
@Autowired
private MapDirectionClient mapDirectionClient;
/**
*
* @param depLongitude
@ -18,17 +23,9 @@ public class DirectionService {
* @return
*/
public ResponseResult driving(String depLongitude, String depLatitude, String destLongitude, String destLatitude){
log.info("出发地经度:"+depLongitude);
log.info("出发地纬度:"+depLatitude);
log.info("目的地经度:"+destLongitude);
log.info("目的地纬度:"+destLatitude);
DirectionResponse directionResponse = new DirectionResponse();
directionResponse.setDistance(123);
directionResponse.setDuration(11);
return ResponseResult.success(directionResponse);
// 调用 第三方 地图服务接口
DirectionResponse direction = mapDirectionClient.direction(depLongitude,depLatitude,destLongitude,destLatitude);
return ResponseResult.success(direction);
}
}

@ -2,4 +2,6 @@ server:
port: 8085
spring:
application:
name: service-map
name: service-map
amap:
key: c5a197858bd6d73cf94cf49430fbe9d2
Loading…
Cancel
Save