| | |
| | | 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 = "2f22a10b75e0c24a074de515f3cdcaea"; |
| | | |
| | | /** |
| | | * 功能描述: 根据地址名称得到两个地址间的距离 |
| | |
| | | * @param start 起始位置 |
| | | * @param end 结束位置 |
| | | * @return long 两个地址间的距离 |
| | | * @author isymikasan |
| | | */ |
| | | public static Long getDistanceByAddress(String start, String end) { |
| | | String startLonLat = getLonLat(start).getDatas(); |
| | |
| | | if (reGeoCode.size() > 0) { |
| | | // 在regeocode中拿到 formatted_address 具体位置 |
| | | String formatted = reGeoCode.get("formatted_address").toString(); |
| | | JSONObject addressComponent = reGeoCode.getJSONObject("addressComponent"); |
| | | String adcode = addressComponent.getString("adcode"); |
| | | String s = convertToCityCode(adcode); |
| | | System.err.println("====s"); |
| | | return Result.succeed(formatted, "地址获取成功!"); |
| | | |
| | | } else { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | public static Result<String> getCityCode(String longitude, String latitude) { |
| | | String url; |
| | | try { |
| | | url = "http://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + longitude + "," + latitude |
| | | + "&key=" + GAO_DE_KEY + "&radius=0&extensions=base"; |
| | | // 高德接口返回的是JSON格式的字符串 |
| | | String queryResult = getResponse(url); |
| | | if (ObjectUtils.isNull(queryResult)) { |
| | | return Result.failed("查询结果为空"); |
| | | } |
| | | // 将获取结果转为json 数据 |
| | | JSONObject obj = JSONObject.parseObject(queryResult); |
| | | if (obj.get(GaoDeEnum.STATUS.getCode()).toString().equals(GaoDeEnum.INT_ONE.getCode())) { |
| | | // 如果没有返回-1 |
| | | JSONObject reGeoCode = obj.getJSONObject(GaoDeEnum.RE_GEO_CODE.getCode()); |
| | | if (reGeoCode.size() > 0) { |
| | | // 在regeocode中拿到 formatted_address 具体位置 |
| | | String formatted = reGeoCode.get("formatted_address").toString(); |
| | | JSONObject addressComponent = reGeoCode.getJSONObject("addressComponent"); |
| | | String adcode = addressComponent.getString("adcode"); |
| | | String s = convertToCityCode(adcode); |
| | | System.err.println("====s"); |
| | | return Result.succeed(s, "地址获取成功!"); |
| | | |
| | | } else { |
| | | return Result.failed("未找到相匹配的地址!"); |
| | | } |
| | | } else { |
| | | return Result.failed("请求错误!"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return Result.failed("系统未知异常,请稍后再试"); |
| | | } |
| | | } |
| | | |
| | | public static String convertToCityCode(String districtCode) { |
| | | // 确保输入的字符串长度至少为6位 |
| | | if (districtCode.length() >= 6) { |
| | | // 截取前6位中的前4位作为城市代码 |
| | | return districtCode.substring(0, 4) + "00"; |
| | | } else { |
| | | throw new IllegalArgumentException("Invalid district code length: " + districtCode); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 功能描述: 根据两个定位点的经纬度算出两点间的距离 |
| | | * 经纬度格式为: 经度,纬度 |
| | |
| | | * @return 两个定位点之间的距离 |
| | | */ |
| | | public static Result<Long> getDistance(String startLonLat, String endLonLat) { |
| | | System.err.println("======"+startLonLat+"===="+endLonLat); |
| | | // 返回起始地startAddr与目的地endAddr之间的距离,单位:米 |
| | | String queryUrl = |
| | | "http://restapi.amap.com/v3/distance?key=" + GAO_DE_KEY + "&origins=" + startLonLat |
| | | + "&destination=" |
| | | + endLonLat; |
| | | String queryResult = getResponse(queryUrl); |
| | | System.err.println("======"+queryResult); |
| | | JSONObject job = JSONObject.parseObject(queryResult); |
| | | JSONArray ja = job.getJSONArray("results"); |
| | | JSONObject jobO = JSONObject.parseObject(ja.getString(0)); |
| | | long result = Long.parseLong(jobO.get("distance").toString()); |
| | | return Result.succeed(result, "距离计算成功!"); |
| | | } |
| | | |
| | | /** |
| | | * 功能描述: 根据地址获取城市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 |
| | | + "&output=json&key=" + GAO_DE_KEY; |
| | | String queryResult = getResponse(queryUrl); |
| | | GaoDeAddressInfoVO data = JSONObject.parseObject(queryResult, GaoDeAddressInfoVO.class); |
| | | List<Geocodes> geocodes = data.getGeocodes(); |
| | | if (geocodes==null||geocodes.isEmpty()) |
| | | return Result.failed(null, "计算失败"); |
| | | Geocodes info = geocodes.get(0); |
| | | String adcode = info.getAdcode(); |
| | | String city = info.getCity(); |
| | | String location = info.getLocation(); |
| | | String[] split = location.split(","); |
| | | return Result.succeed(new CityInfoVO(city, adcode,Double.valueOf(split[0]),Double.valueOf(split[1])), "距离计算成功!"); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |