| | |
| | | /** |
| | | * 功能描述: 高德地图Key |
| | | */ |
| | | private static final String GAO_DE_KEY = "37331f325a4f4ea25bc0d4e1900a1730"; |
| | | private static final String GAO_DE_KEY = "2f22a10b75e0c24a074de515f3cdcaea"; |
| | | |
| | | /** |
| | | * 功能描述: 根据地址名称得到两个地址间的距离 |
| | |
| | | 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)); |
| | |
| | | public static Result<CityInfoVO> getAddressInfo(String address) { |
| | | // 返回起始地startAddr与目的地endAddr之间的距离,单位:米 |
| | | String queryUrl = "https://restapi.amap.com/v3/geocode/geo?address=" + address |
| | | + "&key=" + GAO_DE_KEY; |
| | | + "&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(); |
| | | return Result.succeed(new CityInfoVO(city, adcode), "距离计算成功!"); |
| | | String location = info.getLocation(); |
| | | String[] split = location.split(","); |
| | | return Result.succeed(new CityInfoVO(city, adcode,Double.valueOf(split[0]),Double.valueOf(split[1])), "距离计算成功!"); |
| | | } |
| | | |
| | | /** |