From 831e3eeaaf9bdb1060bb90c5f0e220064900a1b3 Mon Sep 17 00:00:00 2001 From: yh <1844516659@qq.com> Date: Mon, 18 Jul 2022 11:56:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=9E=E6=BB=B4=E5=87=BA=E8=A1=8C=E7=BD=91?= =?UTF-8?q?=E7=BA=A6=E8=BD=A62022-service-map=E8=B0=83=E7=94=A8=E9=AB=98?= =?UTF-8?q?=E5=BE=B7=E5=9C=B0=E5=9B=BE=E8=8E=B7=E5=8F=96=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../constant/AmapConfigConstants.java | 16 ++++ .../servicemap/ServiceMapApplication.java | 9 ++ .../servicemap/remote/MapDirectionClient.java | 92 +++++++++++++++++++ .../servicemap/service/DirectionService.java | 19 ++-- .../src/main/resources/application.yml | 4 +- 5 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 internal-common/src/main/java/com/mashibing/internalcommon/constant/AmapConfigConstants.java create mode 100644 service-map/src/main/java/com/mashibing/servicemap/remote/MapDirectionClient.java diff --git a/internal-common/src/main/java/com/mashibing/internalcommon/constant/AmapConfigConstants.java b/internal-common/src/main/java/com/mashibing/internalcommon/constant/AmapConfigConstants.java new file mode 100644 index 0000000..7695afe --- /dev/null +++ b/internal-common/src/main/java/com/mashibing/internalcommon/constant/AmapConfigConstants.java @@ -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"; + +} diff --git a/service-map/src/main/java/com/mashibing/servicemap/ServiceMapApplication.java b/service-map/src/main/java/com/mashibing/servicemap/ServiceMapApplication.java index 88e2da2..2cedd75 100644 --- a/service-map/src/main/java/com/mashibing/servicemap/ServiceMapApplication.java +++ b/service-map/src/main/java/com/mashibing/servicemap/ServiceMapApplication.java @@ -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(); + } + } diff --git a/service-map/src/main/java/com/mashibing/servicemap/remote/MapDirectionClient.java b/service-map/src/main/java/com/mashibing/servicemap/remote/MapDirectionClient.java new file mode 100644 index 0000000..d5832ff --- /dev/null +++ b/service-map/src/main/java/com/mashibing/servicemap/remote/MapDirectionClient.java @@ -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 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; + } + +} diff --git a/service-map/src/main/java/com/mashibing/servicemap/service/DirectionService.java b/service-map/src/main/java/com/mashibing/servicemap/service/DirectionService.java index b09bcf0..1821596 100644 --- a/service-map/src/main/java/com/mashibing/servicemap/service/DirectionService.java +++ b/service-map/src/main/java/com/mashibing/servicemap/service/DirectionService.java @@ -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); } } diff --git a/service-map/src/main/resources/application.yml b/service-map/src/main/resources/application.yml index a08ae9c..de965eb 100644 --- a/service-map/src/main/resources/application.yml +++ b/service-map/src/main/resources/application.yml @@ -2,4 +2,6 @@ server: port: 8085 spring: application: - name: service-map \ No newline at end of file + name: service-map +amap: + key: c5a197858bd6d73cf94cf49430fbe9d2 \ No newline at end of file