parent
86c9839dd8
commit
68ed974927
@ -0,0 +1,12 @@
|
|||||||
|
package com.internal.contant;
|
||||||
|
|
||||||
|
|
||||||
|
public class AmapConfigConstant {
|
||||||
|
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";
|
||||||
|
|
||||||
|
public static final String DIRECTION_URL = "https://restapi.amap.com/v3/direction/driving";
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
package com.taxi.servicemap.remote;
|
||||||
|
|
||||||
|
import com.internal.contant.AmapConfigConstant;
|
||||||
|
import com.internal.dto.ResponseResult;
|
||||||
|
import com.internal.response.DirectionResponse;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
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
|
||||||
|
@Slf4j
|
||||||
|
public class MapDirectionClient {
|
||||||
|
|
||||||
|
@Value("${amap.key}")
|
||||||
|
private java.lang.String mapKey;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
public DirectionResponse direction(String depLongitude, String depLatitude,
|
||||||
|
String destLongitude, String destLatitude) {
|
||||||
|
//组装请求URL
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
stringBuilder.append(AmapConfigConstant.DIRECTION_URL);
|
||||||
|
stringBuilder.append("?");
|
||||||
|
stringBuilder.append("origin=" + depLongitude + "," + depLatitude);
|
||||||
|
stringBuilder.append("&");
|
||||||
|
stringBuilder.append("destination=" + destLongitude + "," + destLatitude);
|
||||||
|
stringBuilder.append("&output=json");
|
||||||
|
stringBuilder.append("&");
|
||||||
|
stringBuilder.append("key=" + mapKey);
|
||||||
|
log.info(stringBuilder.toString());
|
||||||
|
//调用高德地图API
|
||||||
|
ResponseEntity<String> forEntity =
|
||||||
|
restTemplate.getForEntity(stringBuilder.toString(), String.class);
|
||||||
|
String directionStr = forEntity.getBody();
|
||||||
|
|
||||||
|
//解析
|
||||||
|
DirectionResponse directionResponse = parseDirectionEntity(directionStr);
|
||||||
|
return directionResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DirectionResponse parseDirectionEntity(String directionStr) {
|
||||||
|
DirectionResponse driectionResponse = new DirectionResponse();
|
||||||
|
try {
|
||||||
|
//最外层
|
||||||
|
JSONObject jsonObject = JSONObject.fromObject(directionStr);
|
||||||
|
|
||||||
|
if (jsonObject.has(AmapConfigConstant.STATUS)) {
|
||||||
|
int status = jsonObject.getInt(AmapConfigConstant.STATUS);
|
||||||
|
if (1 == status) {//返回成功
|
||||||
|
if (jsonObject.has(AmapConfigConstant.ROUTE)) {
|
||||||
|
JSONObject routeObj = jsonObject.getJSONObject(AmapConfigConstant.ROUTE);
|
||||||
|
if (routeObj.has(AmapConfigConstant.PATHS)) {
|
||||||
|
JSONArray pathsArray = routeObj.getJSONArray(AmapConfigConstant.PATHS);
|
||||||
|
JSONObject pathsObj = pathsArray.getJSONObject(0);
|
||||||
|
if (pathsObj.has(AmapConfigConstant.DISTANCE)) {
|
||||||
|
Integer distanceInt = pathsObj.getInt(AmapConfigConstant.DISTANCE);
|
||||||
|
driectionResponse.setDistance(distanceInt);
|
||||||
|
}
|
||||||
|
if (pathsObj.has(AmapConfigConstant.DURATION)) {
|
||||||
|
Integer durationInt = pathsObj.getInt(AmapConfigConstant.DURATION);
|
||||||
|
driectionResponse.setDuration(durationInt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
return driectionResponse;
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,21 @@
|
|||||||
package com.taxi.servicemap.service;
|
package com.taxi.servicemap.service;
|
||||||
|
|
||||||
import com.internal.dto.ResponseResult;
|
import com.internal.dto.ResponseResult;
|
||||||
import com.internal.response.DriectionResponse;
|
import com.internal.response.DirectionResponse;
|
||||||
|
import com.taxi.servicemap.remote.MapDirectionClient;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class DirectionSevice {
|
public class DirectionSevice {
|
||||||
|
|
||||||
public ResponseResult driving(String depLongitude, String depLatitude,
|
@Autowired
|
||||||
String destLongitude, String destLatitude){
|
private MapDirectionClient mapDirectionClient;
|
||||||
|
|
||||||
DriectionResponse driectionResponse= new DriectionResponse();
|
public ResponseResult driving(String depLongitude, String depLatitude,
|
||||||
driectionResponse.setDistance(123);
|
String destLongitude, String destLatitude) {
|
||||||
driectionResponse.setDuration(2342);
|
DirectionResponse driectionResponse = mapDirectionClient.direction(depLongitude, depLatitude,
|
||||||
|
destLongitude, destLatitude);
|
||||||
return ResponseResult.success(driectionResponse);
|
return ResponseResult.success(driectionResponse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8085
|
port: 8085
|
||||||
|
|
||||||
|
amap:
|
||||||
|
key: 01d85ea8f7db65efc370cfec18ff6397
|
@ -1,2 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8085
|
port: 8085
|
||||||
|
|
||||||
|
amap:
|
||||||
|
key: 01d85ea8f7db65efc370cfec18ff6397
|
Loading…
Reference in new issue