|
|
@@ -1,57 +1,58 @@
|
|
|
package com.sckw.core.utils;
|
|
|
|
|
|
-import java.net.URL;
|
|
|
-import java.util.StringJoiner;
|
|
|
+import java.io.IOException;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.sckw.core.model.constant.Global;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
public class LocUtils {
|
|
|
|
|
|
- public static String getAddressName(String point) {
|
|
|
- // 返回数据
|
|
|
- String res = "";
|
|
|
- if (point == null || point.equals("0.0,0.0")) {
|
|
|
- return res;
|
|
|
+ static String GD_KEY = "739ace2ede6fe1e2f4ae3bcba890833c";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param lng 经度
|
|
|
+ * @param lat 纬度
|
|
|
+ * @desc 通过gps查询位置信息-高德
|
|
|
+ * @author zk
|
|
|
+ * @date 2023/10/7
|
|
|
+ **/
|
|
|
+ public static String regeo(String lng, String lat) {
|
|
|
+ if (StringUtils.isBlank(lng) || StringUtils.isBlank(lat)) {
|
|
|
+ return null;
|
|
|
}
|
|
|
- // 请求头
|
|
|
- String urlHead = "http://restapi.amap.com/v3/geocode/regeo?key=8325164e247e15eea68b59e89200988b&s=rsv3&location=";
|
|
|
- // 请求参数
|
|
|
- String urlParam = "&radius=2800&callback=jsonp_452865_&platform=JS&logversion=2.0&sdkversion=1.3&appname=http%3A%2F%2Flbs.amap.com%2Fconsole%2Fshow%2Fpicker&csid=49851531-2AE3-4A3B-A8C8-675A69BCA316";
|
|
|
- StringJoiner sj = new StringJoiner("");
|
|
|
- // 完整请求地址
|
|
|
- String requestUrl = sj.add(urlHead).add(point).add(urlParam).toString();
|
|
|
+ StringBuilder requestUrl = new StringBuilder();
|
|
|
+ requestUrl.append("https://restapi.amap.com/v3/geocode/regeo?output=json&location="+ lng + Global.COMMA + lat);
|
|
|
+ requestUrl.append("&key="+GD_KEY);
|
|
|
+ requestUrl.append("&radius=1000&extensions=base");
|
|
|
|
|
|
- String methodStr = "GET";
|
|
|
- String codeType = "UTF-8";
|
|
|
try {
|
|
|
- URL url = new URL(requestUrl);
|
|
|
- java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
|
|
|
- conn.setDoOutput(true);
|
|
|
- conn.setRequestMethod(methodStr);
|
|
|
- java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), codeType));
|
|
|
- String line;
|
|
|
- while ((line = in.readLine()) != null) {
|
|
|
- res += line + "\n";
|
|
|
+ HttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpGet httpGet = new HttpGet(requestUrl.toString());
|
|
|
+ HttpResponse response = httpClient.execute(httpGet);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ String responseStr = EntityUtils.toString(entity, "UTF-8");
|
|
|
+ JSONObject resultJson = responseStr != null ? JSON.parseObject(responseStr) : null;
|
|
|
+ Integer status = resultJson != null ? resultJson.getInteger("status") : null;
|
|
|
+ if (status != null && status == Global.NUMERICAL_ONE) {
|
|
|
+ JSONObject regeocode = resultJson.getJSONObject("regeocode");
|
|
|
+ return regeocode != null ? regeocode.getString("formatted_address") : null;
|
|
|
}
|
|
|
- in.close();
|
|
|
|
|
|
- if (!"".equalsIgnoreCase(res)) {
|
|
|
- String regeoKey = "regeocode";
|
|
|
- String addressKey = "formatted_address";
|
|
|
- String strAddrs = res.substring(res.indexOf("(") + 1, res.lastIndexOf(")"));
|
|
|
- JSONObject addJson = JSON.parseObject(strAddrs);
|
|
|
- JSONObject regeocodeObject = (null != addJson ? addJson.getJSONObject(regeoKey) : null);
|
|
|
- res = (null != regeocodeObject ? regeocodeObject.getString(addressKey) : "");
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ } catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
- return res;
|
|
|
}
|
|
|
- return res;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- String res = getAddressName("118.879289,38.052237");
|
|
|
+ String res = regeo("118.879289", "38.052237");
|
|
|
System.out.println(res);
|
|
|
}
|
|
|
}
|