| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.dsh.other.util.httpClinet.HttpClientUtil; |
| | | import com.dsh.other.util.httpClinet.HttpResult; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | |
| | | public class GDMapGeocodingUtil { |
| | | |
| | | private String key = "fb131ad2dbfb3f39d7d37d244b92aa2d"; |
| | | |
| | | @Autowired |
| | | private HttpClientUtil httpClientUtil; |
| | | |
| | | |
| | | /** |
| | | * 将行政区域名称转化为坐标 |
| | |
| | | |
| | | String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON"; |
| | | url += "&address=" + province + (ToolUtil.isNotEmpty(city) ? city : "") + (ToolUtil.isNotEmpty(county) ? county : "") + (ToolUtil.isNotEmpty(address) ? address : ""); |
| | | HttpResult httpResult = httpClientUtil.pushHttpRequset("GET", url, null, null, "json"); |
| | | HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json"); |
| | | JSONObject jsonObject = JSON.parseObject(httpResult.getData()); |
| | | String status = jsonObject.getString("status"); |
| | | List<String> list = new ArrayList<>(); |
| | | |
| | | // gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/geo", "行政区域转经纬度"); |
| | | |
| | | if(status.equals("1")){ |
| | | JSONArray geocodes = jsonObject.getJSONArray("geocodes"); |
| | |
| | | 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"); |
| | | HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json"); |
| | | JSONObject jsonObject = JSON.parseObject(httpResult.getData()); |
| | | String status = jsonObject.getString("status"); |
| | | List<String> list = new ArrayList<>(); |
| | | |
| | | // gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/geo", "行政区域转经纬度"); |
| | | |
| | | if(status.equals("1")){ |
| | | JSONArray geocodes = jsonObject.getJSONArray("geocodes"); |
| | |
| | | */ |
| | | 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"); |
| | | HttpResult httpResult = HttpClientUtil.pushHttpRequset("GET", url, null, null, "json"); |
| | | JSONObject jsonObject = JSON.parseObject(httpResult.getData()); |
| | | Map<String, String> map = new HashMap<>(); |
| | | |
| | | // gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/regeo", "经纬度转行政区域"); |
| | | |
| | | if(jsonObject.getString("status").equals("1")){ |
| | | JSONObject regeocode = jsonObject.getJSONObject("regeocode"); |
| | |
| | | } |
| | | return map; |
| | | } |
| | | @Autowired |
| | | private GDMapGeocodingUtil gdMapGeocodingUtil; |
| | | public Map<String, String> geocode1(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<>(); |
| | | |
| | | Map<String, String> geocode = gdMapGeocodingUtil.geocode(lon, lan); |
| | | String provinceCode = geocode.get("provinceCode"); |
| | | String cityCode = geocode.get("cityCode"); |
| | | map.put("cityCode", cityCode); |
| | | |
| | | // 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"); |
| | | // map.put("province", province); |
| | | // map.put("provinceCode", code.substring(0, 2) + "0000"); |
| | | // map.put("city", city); |
| | | // map.put("cityCode", code.substring(0, 4) + "00"); |
| | | // map.put("district", district); |
| | | // map.put("districtCode", code); |
| | | // } |
| | | return map; |
| | | } |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | 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"); |
| | | 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"))){ |
| | |
| | | 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); |
| | | double demo = result / 1000; |
| | | return decimalFormat.format(demo); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(); |
| | | } |