LocationUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.ruoyi.common.utils;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.apache.http.client.methods.CloseableHttpResponse;
  5. import org.apache.http.client.methods.HttpGet;
  6. import org.apache.http.client.utils.URIBuilder;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.util.EntityUtils;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Component;
  13. import java.net.URI;
  14. import java.util.Map;
  15. @Component
  16. public class LocationUtils {
  17. /**
  18. * 日志对象
  19. */
  20. private static final Logger logger = LoggerFactory.getLogger(LocationUtils.class);
  21. private String KEY = "67019de565fb4aa2a0e101b70ae3fd91";
  22. /**
  23. * 地理编码的url
  24. */
  25. public final String LOCATION_URL = "https://restapi.amap.com/v3/geocode/geo?parameters";
  26. /**
  27. * 逆地理编码的url
  28. */
  29. public final String COUNTER_LOCATION_URL = "https://restapi.amap.com/v3/geocode/regeo?parameters";
  30. /**
  31. * 发送get请求
  32. *
  33. * @return
  34. */
  35. public JSONObject getLocation(Map<String, String> params) {
  36. JSONObject jsonObject = null;
  37. CloseableHttpClient httpclient = HttpClients.createDefault();
  38. // 创建URI对象,并且设置请求参数
  39. try {
  40. URI uri = getBuilderLocation(LOCATION_URL, params);
  41. // 创建http GET请求
  42. HttpGet httpGet = new HttpGet(uri);
  43. CloseableHttpResponse response = httpclient.execute(httpGet);
  44. // 判断返回状态是否为200
  45. jsonObject = getLocation(response);
  46. httpclient.close();
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. return jsonObject;
  51. }
  52. /**
  53. * 根据地址获取到经纬度
  54. *
  55. * @param response
  56. * @return
  57. */
  58. private static JSONObject getLocation(CloseableHttpResponse response) throws Exception {
  59. JSONObject geocode = null;
  60. // 判断返回状态是否为200
  61. if (response.getStatusLine().getStatusCode() == 200) {
  62. String content = EntityUtils.toString(response.getEntity(), "UTF-8");
  63. logger.info("调用高德地图接口返回的结果为:{}", content);
  64. JSONObject jsonObject = (JSONObject) JSONObject.parse(content);
  65. JSONArray geocodes = (JSONArray) jsonObject.get("geocodes");
  66. geocode = (JSONObject) geocodes.get(0);
  67. logger.info("返回的结果为:{}", JSONObject.toJSONString(geocode));
  68. }
  69. return geocode;
  70. }
  71. /**
  72. * 地理编码封装URI
  73. *
  74. * @param url
  75. * @param params
  76. * @return
  77. * @throws Exception
  78. */
  79. private URI getBuilderLocation(String url, Map<String, String> params) throws Exception {
  80. // 详细地址
  81. String address = params.get("address");
  82. String city = params.get("city");
  83. URIBuilder uriBuilder = new URIBuilder(url);
  84. // 公共参数
  85. uriBuilder.setParameter("key", KEY);
  86. uriBuilder.setParameter("address", address);
  87. uriBuilder.setParameter("city", city);
  88. logger.info("请求的参数为:{}", JSONObject.toJSONString(uriBuilder));
  89. URI uri = uriBuilder.build();
  90. return uri;
  91. }
  92. /**
  93. * 逆地理编码
  94. *
  95. * @return
  96. */
  97. public JSONObject getCounterLocation(Map<String, String> params) {
  98. JSONObject jsonObject = null;
  99. CloseableHttpClient httpclient = HttpClients.createDefault();
  100. // 创建URI对象,并且设置请求参数
  101. try {
  102. URI uri = getBuilderCounterLocation(COUNTER_LOCATION_URL, params);
  103. // 创建http GET请求
  104. HttpGet httpGet = new HttpGet(uri);
  105. CloseableHttpResponse response = httpclient.execute(httpGet);
  106. // 判断返回状态是否为200
  107. jsonObject = getCounterLocation(response);
  108. httpclient.close();
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. return jsonObject;
  113. }
  114. /**
  115. * 逆地理编码封装URI
  116. *
  117. * @param url
  118. * @param params
  119. * @return
  120. * @throws Exception
  121. */
  122. private URI getBuilderCounterLocation(String url, Map<String, String> params) throws Exception {
  123. // 详细地址
  124. String location = params.get("location");
  125. URIBuilder uriBuilder = new URIBuilder(url);
  126. // 公共参数
  127. uriBuilder.setParameter("key", KEY);
  128. uriBuilder.setParameter("location", location);
  129. logger.info("请求的参数为:{}", JSONObject.toJSONString(uriBuilder));
  130. URI uri = uriBuilder.build();
  131. return uri;
  132. }
  133. /**
  134. * 根据地址获取到经纬度
  135. *
  136. * @param response
  137. * @return
  138. */
  139. private static JSONObject getCounterLocation(CloseableHttpResponse response) throws Exception {
  140. JSONObject regeocode = null;
  141. // 判断返回状态是否为200
  142. if (response.getStatusLine().getStatusCode() == 200) {
  143. String content = EntityUtils.toString(response.getEntity(), "UTF-8");
  144. logger.info("调用高德地图接口返回的结果为:{}", content);
  145. JSONObject jsonObject = (JSONObject) JSONObject.parse(content);
  146. regeocode = (JSONObject) jsonObject.get("regeocode");
  147. logger.info("返回的结果为:{}", JSONObject.toJSONString(regeocode));
  148. }
  149. return regeocode;
  150. }
  151. }