package com.supersavedriving.driver.modular.system.util.GaoDe;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.supersavedriving.driver.modular.system.util.GaoDe.model.District;
|
import com.supersavedriving.driver.modular.system.util.httpClinet.HttpClientUtil;
|
import com.supersavedriving.driver.modular.system.util.httpClinet.HttpResult;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 地图工具类
|
* @author pzb
|
* @Date 2023/2/16 18:48
|
*/
|
public class MapUtil {
|
|
public static Logger logger = LoggerFactory.getLogger("ServiceLog");
|
|
|
|
|
/**
|
* 获取两点的距离
|
* @param origins 起点坐标
|
* @param destination 终点坐标
|
* @param type 计算类型:0:直线距离 1:驾车导航距离(仅支持国内坐标)。
|
* @return
|
*/
|
public static Map<String, String> getDistance(String origins, String destination, Integer type){
|
try {
|
String url = "https://restapi.amap.com/v3/distance?key=" + MapConfig.key + "&origins=" + origins + "&destination=" + destination +
|
"&type=" + type;
|
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
|
if(httpResult.getCode() != 200){
|
logger.debug(httpResult.getData());
|
return null;
|
}
|
String data = httpResult.getData();
|
JSONObject jsonObject = JSON.parseObject(data);
|
String status = jsonObject.getString("status");
|
if(status.equals("1")){
|
JSONArray results = jsonObject.getJSONArray("results");
|
JSONObject jsonObject1 = results.getJSONObject(0);
|
Map<String, String> map = new HashMap<>();
|
map.put("distance", jsonObject1.getString("distance"));//距离(米)
|
map.put("duration", jsonObject1.getString("duration"));//预计时间(秒)
|
return map;
|
}else{
|
logger.debug(data);
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
/**
|
* 地址转换经纬度
|
* @param address
|
* @return
|
*/
|
public static List<String> geocoding(String address){
|
try {
|
String url = "https://restapi.amap.com/v3/geocode/geo?key=" + MapConfig.key + "&output=JSON&address=" + address;
|
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
|
if(httpResult.getCode() != 200){
|
return null;
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
String status = jsonObject.getString("status");
|
List<String> list = new ArrayList<>();
|
if(status.equals("1")){
|
JSONArray geocodes = jsonObject.getJSONArray("geocodes");
|
for(int i = 0; i < geocodes.size(); i++){
|
String location = geocodes.getJSONObject(i).getString("location");
|
list.add(location);
|
}
|
}
|
return list;
|
}catch (Exception e){
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
|
|
/**
|
* 根据经纬度获取行政区域信息
|
* @param lon
|
* @param lan
|
* @return
|
* @throws Exception
|
*/
|
public static District geocode(String lon, String lan) {
|
try {
|
String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + MapConfig.key + "&location=" + lon + "," + lan;
|
HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json");
|
if(httpResult.getCode() != 200){
|
return null;
|
}
|
JSONObject jsonObject = JSON.parseObject(httpResult.getData());
|
Map<String, String> map = new HashMap<>();
|
if(jsonObject.getString("status").equals("1")){
|
JSONObject regeocode = jsonObject.getJSONObject("regeocode");
|
JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
|
String address = regeocode.getString("formatted_address");
|
map.put("address", address);
|
String code = addressComponent.getString("adcode");
|
String province = addressComponent.getString("province");
|
String city = addressComponent.getString("city");
|
String district = addressComponent.getString("district");
|
District district1 = new District();
|
district1.setProvince(province);
|
district1.setProvinceCode(code.substring(0, 2) + "0000");
|
district1.setCity(city);
|
district1.setCityCode(code.substring(0, 4) + "00");
|
district1.setDistrict(district);
|
district1.setDistrictCode(code);
|
return district1;
|
}
|
logger.debug(httpResult.getData());
|
return null;
|
}catch (Exception e){
|
e.printStackTrace();
|
return null;
|
}
|
}
|
}
|