获取key
通过console.amap.com/dev/key/app 获取高德认证的key
得到key
测试key
测试地址
找到
然后将自己的复制上去,并根据自己的需要的格式将链接中的output设置为xml或者JSON
测试成功
调用地图服务
实现业务
根据起点和终点经纬度获取距离(米)和时长(分钟)
前期准备
- 将前文中获取的
key写入.yml配置文件
amap:
key: 9edd7a0c343d993fc971ad2afda94dd5
- 并且在启动类中注入
RestTemplate
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
- 创建实体类常量用来保存
url
public class AMapConfigConstant {
/**
* 路径规划地址
*/
public static final String mapUrl="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";
}
编写MapDirectionClient类
在remote包下创建MapDirectionClient类来调用高德地图接口
其中,方法parseDirectionEntity()解析JSON的过程可以根据此图来参考
package com.whj.servicemap.remote;
import com.whj.internalcommon.constant.AMapConfigConstant;
import com.whj.internalcommon.response.DirectionResponse;
import lombok.extern.slf4j.Slf4j;
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;
/**
* @Auther: wanghaijun
* @Date: 2023/1/6 - 01 - 06 - 15:10
* @Description: com.whj.servicemap.remote
*/
@Service
@Slf4j
public class MapDirectionClient {
@Value("${amap.key}")
private String amapKey;
@Autowired
private RestTemplate restTemplate;
/**
* 根据出发的经纬度获取距离和时间
* @param depLongitude
* @param depLatitude
* @param desLongitude
* @param desLatitude
* @return
*/
public DirectionResponse direction(String depLongitude, String depLatitude, String desLongitude, String desLatitude) {
//组装url
StringBuilder urlBuild = new StringBuilder();
urlBuild.append(AMapConfigConstant.mapUrl);
urlBuild.append("?");
urlBuild.append("origin=" + depLongitude + "," + depLatitude);
urlBuild.append("&");
urlBuild.append("destination=" + desLongitude + "," + desLatitude);
urlBuild.append("&");
urlBuild.append("extensions=all");
urlBuild.append("&");
urlBuild.append("output=json");
urlBuild.append("&");
urlBuild.append("key=" + amapKey); //key 9edd7a0c343d993fc971ad2afda94dd5
//urlBuild = https://restapi.amap.com/v3/direction/driving?origin=116.481028,39.989643&destination=116.465302,40.004717&extensions=all&output=json&key=9edd7a0c343d993fc971ad2afda94dd5
log.info(urlBuild.toString());
//调用高德的接口
ResponseEntity<String> directionForEntity = restTemplate.getForEntity(urlBuild.toString(), String.class);
String directionString = directionForEntity.getBody();
log.info("高德地图,路径规划,返回信息" + directionString);
//解析接口
DirectionResponse directionResponse = parseDirectionEntity(directionString);
return directionResponse;
}
/**
* 根据传来的字符串获取指定的参数的数据 解析数据
* @param directionString
* @return
*/
private DirectionResponse parseDirectionEntity(String directionString) {
DirectionResponse directionResponse = null;
try {
//将获取的对象转化为JSON格式
JSONObject result = JSONObject.fromObject(directionString);
if(result.has(AMapConfigConstant.STATUS)){
int status = result.getInt(AMapConfigConstant.STATUS);
if(status==1){
if(result.has(AMapConfigConstant.ROUTE)){
JSONObject routeObject = result.getJSONObject(AMapConfigConstant.ROUTE);
JSONArray pathsArray = routeObject.getJSONArray(AMapConfigConstant.PATHS);
//获取ROUTE 数组中的第一个json
JSONObject pathObject = pathsArray.getJSONObject(0);
//创建接收对象
directionResponse = new DirectionResponse();
//设置距离
if(pathObject.has(AMapConfigConstant.DISTANCE)){
int distance = pathObject.getInt(AMapConfigConstant.DISTANCE);
directionResponse.setDistance(distance);
}
//设置时间
if (pathObject.has(AMapConfigConstant.DURATION)){
int duration = pathObject.getInt(AMapConfigConstant.DURATION);
directionResponse.setDuration(duration);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return directionResponse;
}
}
service层
@Service
public class DirectionServiceImpl implements DirectionService {
@Autowired
private MapDirectionClient mapDirectionClient;
/**
* 根据起点和终点经纬度获取距离(米)和时长(分钟)
*
* @param depLongitude
* @param depLatitude
* @param desLongitude
* @param desLatitude
* @return
*/
@Override
public ResponseResult driving(String depLongitude, String depLatitude, String desLongitude, String desLatitude) {
//调用第三方地图接口
DirectionResponse directionResponse = mapDirectionClient.direction(depLongitude, depLatitude, desLongitude, desLatitude);
return ResponseResult.success(directionResponse);
}
}