| | |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.ruoyi.common.core.domain.Result; |
| | | import com.ruoyi.common.core.enums.GaoDeEnum; |
| | | import com.ruoyi.common.core.exception.GlobalException; |
| | | import com.ruoyi.common.core.vo.*; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.net.URL; |
| | | import java.net.URLConnection; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author HJL |
| | |
| | | /** |
| | | * 功能描述: 高德地图Key |
| | | */ |
| | | private static final String GAO_DE_KEY = "3f00f94b6e9f470a5c6f510c3df814ea"; |
| | | |
| | | //申请的账户Key |
| | | private static final String GAO_DE_KEY = "37331f325a4f4ea25bc0d4e1900a1730"; |
| | | |
| | | /** |
| | | * 功能描述: 根据地址名称得到两个地址间的距离 |
| | |
| | | * @param start 起始位置 |
| | | * @param end 结束位置 |
| | | * @return long 两个地址间的距离 |
| | | * @author isymikasan |
| | | */ |
| | | public static Long getDistanceByAddress(String start, String end) { |
| | | String startLonLat = getLonLat(start).getDatas(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 功能描述: 根据地址获取城市code |
| | | * |
| | | * @param address d地址 |
| | | * @return 两个定位点之间的距离 |
| | | */ |
| | | public static Result<CityInfoVO> getAddressInfo(String address) { |
| | | // 返回起始地startAddr与目的地endAddr之间的距离,单位:米 |
| | | String queryUrl = "https://restapi.amap.com/v3/geocode/geo?address=" + address |
| | | + "&key=" + GAO_DE_KEY; |
| | | String queryResult = getResponse(queryUrl); |
| | | GaoDeAddressInfoVO data = JSONObject.parseObject(queryResult, GaoDeAddressInfoVO.class); |
| | | List<Geocodes> geocodes = data.getGeocodes(); |
| | | Geocodes info = geocodes.get(0); |
| | | String adcode = info.getAdcode(); |
| | | String city = info.getCity(); |
| | | return Result.succeed(new CityInfoVO(city, adcode), "距离计算成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 功能描述: 发送请求 |
| | | * |
| | | * @param serverUrl 请求地址 |
| | |
| | | return result.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 从出发地触发到目的地路线规划 |
| | | * |
| | | * @param origin 出发地经纬度 |
| | | * @param destination 目的地经纬度 |
| | | * @return 路线规划信息 |
| | | */ |
| | | public static Path routing(String origin, String destination) { |
| | | String baseUrl = "https://restapi.amap.com/v3/direction/driving?origin=%s&destination=%s" + |
| | | "&extensions=all&output=json&key=%s"; |
| | | String requestUrl = String.format(baseUrl, origin, destination, GAO_DE_KEY); |
| | | StringBuilder json = new StringBuilder(); |
| | | try { |
| | | URL url = new URL(requestUrl); |
| | | URLConnection urlConnection = url.openConnection(); |
| | | BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), |
| | | StandardCharsets.UTF_8)); |
| | | String inputLine; |
| | | while ((inputLine = in.readLine()) != null) { |
| | | json.append(inputLine); |
| | | } |
| | | in.close(); |
| | | } catch (IOException e) { |
| | | throw new GlobalException("获取路线规划失败!"); |
| | | } |
| | | String data = json.toString(); |
| | | GaoDeMap gaoDeMap = JSONObject.parseObject(data, GaoDeMap.class); |
| | | Path path = gaoDeMap.getRoute().getPaths().get(0); |
| | | List<Steps> steps = path.getSteps(); |
| | | for (Steps step : steps) { |
| | | // 经纬度地址指向 |
| | | List<PolylineData> polylineDataList = new ArrayList<>(); |
| | | String polyline = step.getPolyline(); |
| | | List<String> list = Arrays.stream(polyline.split(";")).collect(Collectors.toList()); |
| | | for (String s : list) { |
| | | String[] split = s.split(","); |
| | | // 经纬度 |
| | | String longitude = split[0]; |
| | | String latitude = split[1]; |
| | | polylineDataList.add(new PolylineData(longitude, latitude)); |
| | | } |
| | | step.setPolylineList(polylineDataList); |
| | | } |
| | | return path; |
| | | } |
| | | |
| | | } |