|
@@ -18,7 +18,9 @@ import java.util.Map;
|
|
|
@Component
|
|
|
public class LocationUtils {
|
|
|
|
|
|
- /**日志对象*/
|
|
|
+ /**
|
|
|
+ * 日志对象
|
|
|
+ */
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LocationUtils.class);
|
|
|
|
|
|
private String KEY = "67019de565fb4aa2a0e101b70ae3fd91";
|
|
@@ -35,9 +37,10 @@ public class LocationUtils {
|
|
|
|
|
|
/**
|
|
|
* 发送get请求
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
- public JSONObject getLocation(Map<String, String> params){
|
|
|
+ public JSONObject getLocation(Map<String, String> params) {
|
|
|
|
|
|
JSONObject jsonObject = null;
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
@@ -61,32 +64,34 @@ public class LocationUtils {
|
|
|
|
|
|
/**
|
|
|
* 根据地址获取到经纬度
|
|
|
+ *
|
|
|
* @param response
|
|
|
* @return
|
|
|
*/
|
|
|
- private static JSONObject getLocation(CloseableHttpResponse response) throws Exception{
|
|
|
+ private static JSONObject getLocation(CloseableHttpResponse response) throws Exception {
|
|
|
JSONObject geocode = null;
|
|
|
// 判断返回状态是否为200
|
|
|
if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
- logger.info("调用高德地图接口返回的结果为:{}",content);
|
|
|
+ logger.info("调用高德地图接口返回的结果为:{}", content);
|
|
|
JSONObject jsonObject = (JSONObject) JSONObject.parse(content);
|
|
|
JSONArray geocodes = (JSONArray) jsonObject.get("geocodes");
|
|
|
geocode = (JSONObject) geocodes.get(0);
|
|
|
|
|
|
- logger.info("返回的结果为:{}",JSONObject.toJSONString(geocode));
|
|
|
+ logger.info("返回的结果为:{}", JSONObject.toJSONString(geocode));
|
|
|
}
|
|
|
return geocode;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 地理编码封装URI
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- private URI getBuilderLocation(String url, Map<String, String> params) throws Exception{
|
|
|
+ private URI getBuilderLocation(String url, Map<String, String> params) throws Exception {
|
|
|
// 详细地址
|
|
|
String address = params.get("address");
|
|
|
String city = params.get("city");
|
|
@@ -104,38 +109,36 @@ public class LocationUtils {
|
|
|
|
|
|
/**
|
|
|
* 逆地理编码
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
- public JSONObject getCounterLocation(Map<String, String> params){
|
|
|
-
|
|
|
+ public JSONObject getCounterLocation(Map<String, String> params) {
|
|
|
JSONObject jsonObject = null;
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
-
|
|
|
// 创建URI对象,并且设置请求参数
|
|
|
try {
|
|
|
URI uri = getBuilderCounterLocation(COUNTER_LOCATION_URL, params);
|
|
|
// 创建http GET请求
|
|
|
HttpGet httpGet = new HttpGet(uri);
|
|
|
CloseableHttpResponse response = httpclient.execute(httpGet);
|
|
|
-
|
|
|
// 判断返回状态是否为200
|
|
|
jsonObject = getCounterLocation(response);
|
|
|
httpclient.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
-
|
|
|
return jsonObject;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 逆地理编码封装URI
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- private URI getBuilderCounterLocation(String url, Map<String, String> params) throws Exception{
|
|
|
+ private URI getBuilderCounterLocation(String url, Map<String, String> params) throws Exception {
|
|
|
// 详细地址
|
|
|
String location = params.get("location");
|
|
|
|
|
@@ -151,19 +154,20 @@ public class LocationUtils {
|
|
|
|
|
|
/**
|
|
|
* 根据地址获取到经纬度
|
|
|
+ *
|
|
|
* @param response
|
|
|
* @return
|
|
|
*/
|
|
|
- private static JSONObject getCounterLocation(CloseableHttpResponse response) throws Exception{
|
|
|
+ private static JSONObject getCounterLocation(CloseableHttpResponse response) throws Exception {
|
|
|
JSONObject regeocode = null;
|
|
|
// 判断返回状态是否为200
|
|
|
if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
- logger.info("调用高德地图接口返回的结果为:{}",content);
|
|
|
+ logger.info("调用高德地图接口返回的结果为:{}", content);
|
|
|
JSONObject jsonObject = (JSONObject) JSONObject.parse(content);
|
|
|
- regeocode = (JSONObject)jsonObject.get("regeocode");
|
|
|
+ regeocode = (JSONObject) jsonObject.get("regeocode");
|
|
|
|
|
|
- logger.info("返回的结果为:{}",JSONObject.toJSONString(regeocode));
|
|
|
+ logger.info("返回的结果为:{}", JSONObject.toJSONString(regeocode));
|
|
|
}
|
|
|
return regeocode;
|
|
|
}
|