|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 将行政区域名称转化为坐标 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param province | 
|---|
|  |  |  | * @param city | 
|---|
|  |  |  | * @param county | 
|---|
|  |  |  | * @param address | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Map<String, Object> geocoding(String province, String city, String county, String address) throws Exception{ | 
|---|
|  |  |  | public Map<String, Object> geocoding(String province, String city, String county, String address) throws Exception { | 
|---|
|  |  |  | Map<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | if(ToolUtil.isEmpty(province)){ | 
|---|
|  |  |  | if (ToolUtil.isEmpty(province)) { | 
|---|
|  |  |  | map.put("status", -1); | 
|---|
|  |  |  | map.put("data", "省不能为空"); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if((ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(county)) || (ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(address))){ | 
|---|
|  |  |  | if ((ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(county)) || (ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(address))) { | 
|---|
|  |  |  | map.put("status", -1); | 
|---|
|  |  |  | map.put("data", "市不能为空"); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if((ToolUtil.isEmpty(county) && ToolUtil.isNotEmpty(address))){ | 
|---|
|  |  |  | if ((ToolUtil.isEmpty(county) && ToolUtil.isNotEmpty(address))) { | 
|---|
|  |  |  | map.put("status", -1); | 
|---|
|  |  |  | map.put("data", "县/区不能为空"); | 
|---|
|  |  |  | return map; | 
|---|
|  |  |  | 
|---|
|  |  |  | String status = jsonObject.getString("status"); | 
|---|
|  |  |  | List<String> list = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(status.equals("1")){ | 
|---|
|  |  |  | if (status.equals("1")) { | 
|---|
|  |  |  | JSONArray geocodes = jsonObject.getJSONArray("geocodes"); | 
|---|
|  |  |  | for(int i = 0; i < geocodes.size(); i++){ | 
|---|
|  |  |  | for (int i = 0; i < geocodes.size(); i++) { | 
|---|
|  |  |  | String location = geocodes.getJSONObject(i).getString("location"); | 
|---|
|  |  |  | list.add(location); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Map<String, Object> geocoding(String address) throws Exception{ | 
|---|
|  |  |  | public Map<String, Object> geocoding(String address) throws Exception { | 
|---|
|  |  |  | Map<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON&address=" + address; | 
|---|
|  |  |  | HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json"); | 
|---|
|  |  |  | 
|---|
|  |  |  | String status = jsonObject.getString("status"); | 
|---|
|  |  |  | List<String> list = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(status.equals("1")){ | 
|---|
|  |  |  | if (status.equals("1")) { | 
|---|
|  |  |  | JSONArray geocodes = jsonObject.getJSONArray("geocodes"); | 
|---|
|  |  |  | for(int i = 0; i < geocodes.size(); i++){ | 
|---|
|  |  |  | for (int i = 0; i < geocodes.size(); i++) { | 
|---|
|  |  |  | String location = geocodes.getJSONObject(i).getString("location"); | 
|---|
|  |  |  | list.add(location); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据经纬度获取行政区域信息 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param lon | 
|---|
|  |  |  | * @param lan | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | * @throws Exception | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Map<String, String> geocode(String lon, String lan) throws Exception{ | 
|---|
|  |  |  | public Map<String, String> geocode(String lon, String lan) throws Exception { | 
|---|
|  |  |  | String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan; | 
|---|
|  |  |  | HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json"); | 
|---|
|  |  |  | JSONObject jsonObject = JSON.parseObject(httpResult.getData()); | 
|---|
|  |  |  | Map<String, String> map = new HashMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(jsonObject.getString("status").equals("1")){ | 
|---|
|  |  |  | if (jsonObject.getString("status").equals("1")) { | 
|---|
|  |  |  | JSONObject regeocode = jsonObject.getJSONObject("regeocode"); | 
|---|
|  |  |  | JSONObject addressComponent = regeocode.getJSONObject("addressComponent"); | 
|---|
|  |  |  | String address = regeocode.getString("formatted_address"); | 
|---|
|  |  |  | 
|---|
|  |  |  | String province = addressComponent.getString("province"); | 
|---|
|  |  |  | String city = addressComponent.getString("city"); | 
|---|
|  |  |  | String district = addressComponent.getString("district"); | 
|---|
|  |  |  | if("[]".equals(code)){ | 
|---|
|  |  |  | System.err.println("经纬度转行政区划代码失败[" + lon + "," + lan + "]"); | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | map.put("province", province); | 
|---|
|  |  |  | map.put("provinceCode", code.substring(0, 2) + "0000"); | 
|---|
|  |  |  | map.put("city", city); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 坐标转换 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param locations 经度和纬度用","分割,经度在前,纬度在后,经纬度小数点后不得超过6位。多个坐标对之间用”|”进行分隔最多支持40对坐标。 | 
|---|
|  |  |  | * @param coordsys  可选值:gps;mapbar;baidu;autonavi(不进行转换) | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | * @throws Exception | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Map<String, String> convert(String locations, String coordsys) throws Exception{ | 
|---|
|  |  |  | public Map<String, String> convert(String locations, String coordsys) throws Exception { | 
|---|
|  |  |  | String url = "https://restapi.amap.com/v3/assistant/coordinate/convert?locations=" + locations + "&coordsys=" + coordsys + "&output=json&key=" + key; | 
|---|
|  |  |  | HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json"); | 
|---|
|  |  |  | JSONObject jsonObject = JSON.parseObject(httpResult.getData()); | 
|---|
|  |  |  | Map<String, String> map = new HashMap<>(); | 
|---|
|  |  |  | if("1".equals(jsonObject.getString("status"))){ | 
|---|
|  |  |  | if ("1".equals(jsonObject.getString("status"))) { | 
|---|
|  |  |  | map.put("code", jsonObject.getString("infocode"));//"10000" | 
|---|
|  |  |  | map.put("info", jsonObject.getString("info"));//status为0时,info返回错误原;否则返回“OK”。 | 
|---|
|  |  |  | map.put("locations", jsonObject.getString("locations").split(";")[0]);//转换之后的坐标。若有多个坐标,则用 “;”进行区分和间隔 | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | map.put("code", jsonObject.getString("infocode")); | 
|---|
|  |  |  | map.put("info", jsonObject.getString("info"));//status为0时,info返回错误原;否则返回“OK”。 | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param endLonLat   结束经纬度(目标经纬度) | 
|---|
|  |  |  | * @return java.lang.Long 两个定位点之间的距离 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public String getDistanceTOKilometer(String startLonLat, String endLonLat) { | 
|---|
|  |  |  | public double getDistanceTOKilometer(String startLonLat, String endLonLat) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | // 返回起始地startAddr与目的地endAddr之间的距离,单位:米 | 
|---|
|  |  |  | Long result = new Long(0); | 
|---|
|  |  |  | 
|---|
|  |  |  | String queryResult = getResponse(queryUrl); | 
|---|
|  |  |  | JSONObject job = JSONObject.parseObject(queryResult); | 
|---|
|  |  |  | JSONArray ja = job.getJSONArray("results"); | 
|---|
|  |  |  | if (ja.size() == 0) { | 
|---|
|  |  |  | return 0.00; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | JSONObject jobO = JSONObject.parseObject(ja.getString(0)); | 
|---|
|  |  |  | result = Long.parseLong(jobO.get("distance").toString()); | 
|---|
|  |  |  | double kilDis = (double) result / 1000; | 
|---|
|  |  |  | DecimalFormat decimalFormat = new DecimalFormat("#.00"); | 
|---|
|  |  |  | return decimalFormat.format(kilDis); | 
|---|
|  |  |  | return (double) result / 1000; | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new RuntimeException(); | 
|---|
|  |  |  | } | 
|---|