| | |
| | | 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; |
| | | import java.io.InputStreamReader; |
| | | import java.net.MalformedURLException; |
| | | import java.net.URL; |
| | | import java.net.URLConnection; |
| | | import java.text.DecimalFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | public class GDMapGeocodingUtil { |
| | | |
| | | private String key = "fb131ad2dbfb3f39d7d37d244b92aa2d"; |
| | | |
| | | |
| | | /** |
| | | * 将行政区域名称转化为坐标 |
| | |
| | | JSONObject jsonObject = JSON.parseObject(httpResult.getData()); |
| | | Map<String, String> map = new HashMap<>(); |
| | | |
| | | |
| | | if(jsonObject.getString("status").equals("1")){ |
| | | JSONObject regeocode = jsonObject.getJSONObject("regeocode"); |
| | | JSONObject addressComponent = regeocode.getJSONObject("addressComponent"); |
| | |
| | | map.put("district", district); |
| | | map.put("districtCode", code); |
| | | } |
| | | 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; |
| | | } |
| | | |
| | |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 功能描述: 根据两个定位点的经纬度算出两点间的距离(米/m) |
| | | * |
| | | * @param startLonLat 起始经纬度 |
| | | * @param endLonLat 结束经纬度(目标经纬度) |
| | | * @return java.lang.Long 两个定位点之间的距离 |
| | | */ |
| | | public Long getDistance(String startLonLat, String endLonLat) { |
| | | try { |
| | | // 返回起始地startAddr与目的地endAddr之间的距离,单位:米 |
| | | Long result = new Long(0); |
| | | String queryUrl = |
| | | "http://restapi.amap.com/v3/distance?key=" + key + "&origins=" + startLonLat |
| | | + "&destination=" |
| | | + endLonLat; |
| | | String queryResult = getResponse(queryUrl); |
| | | JSONObject job = JSONObject.parseObject(queryResult); |
| | | JSONArray ja = job.getJSONArray("results"); |
| | | JSONObject jobO = JSONObject.parseObject(ja.getString(0)); |
| | | result = Long.parseLong(jobO.get("distance").toString()); |
| | | return result; |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 功能描述: 根据两个定位点的经纬度算出两点间的距离(千米/km) |
| | | * |
| | | * @param startLonLat 起始经纬度 |
| | | * @param endLonLat 结束经纬度(目标经纬度) |
| | | * @return java.lang.Long 两个定位点之间的距离 |
| | | */ |
| | | public String getDistanceTOKilometer(String startLonLat, String endLonLat) { |
| | | try { |
| | | // 返回起始地startAddr与目的地endAddr之间的距离,单位:米 |
| | | Long result = new Long(0); |
| | | String queryUrl = |
| | | "http://restapi.amap.com/v3/distance?key=" + key + "&origins=" + startLonLat |
| | | + "&destination=" |
| | | + endLonLat; |
| | | 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()); |
| | | DecimalFormat decimalFormat = new DecimalFormat("#.00"); |
| | | double demo = result / 1000; |
| | | return decimalFormat.format(demo); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 功能描述: 发送请求 |
| | | * |
| | | * @param serverUrl 请求地址 |
| | | * @return java.lang.String |
| | | * @author isymikasan |
| | | * @date 2022-01-26 09:15:01 |
| | | */ |
| | | private static String getResponse(String serverUrl) { |
| | | // 用JAVA发起http请求,并返回json格式的结果 |
| | | StringBuffer result = new StringBuffer(); |
| | | try { |
| | | URL url = new URL(serverUrl); |
| | | URLConnection conn = url.openConnection(); |
| | | BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| | | String line; |
| | | while ((line = in.readLine()) != null) { |
| | | result.append(line); |
| | | } |
| | | in.close(); |
| | | } catch (MalformedURLException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result.toString(); |
| | | } |
| | | |
| | | |
| | | } |