|
@@ -0,0 +1,114 @@
|
|
|
+package com.fdkankan.manage.util;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fdkankan.manage.vo.response.AddressComponent;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class ProvinceUtils {
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ //{
|
|
|
+ // "altitude": 22.34735107421875,
|
|
|
+ // "horizontalAccuracy": 4.9379683,
|
|
|
+ // "latitude": 22.36707762,
|
|
|
+ // "longitude": 113.59580565,
|
|
|
+ // "timestamp": 1638779141699,
|
|
|
+ // "verticalAccuracy": 7.5588307
|
|
|
+ //}
|
|
|
+ String province = pointsToLocationsAll("113.595673,22.36707762");
|
|
|
+ System.out.println(province);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getProvince(String log, String lat ){
|
|
|
+ //lat 小 log 大
|
|
|
+ //参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
|
|
|
+ String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010";
|
|
|
+ String res = "";
|
|
|
+ try {
|
|
|
+ URL url = new URL(urlString);
|
|
|
+ java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ res += line+"\n";
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("error in wapaction,and e is " + e.getMessage());
|
|
|
+ }
|
|
|
+ System.out.println(res);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(res);
|
|
|
+ JSONArray jsonArray = JSON.parseArray(jsonObject.getString("addrList"));
|
|
|
+ JSONObject jsonObject1 = jsonArray.getJSONObject(0);
|
|
|
+ String arr[] = jsonObject1.get("admName").toString().split(",");
|
|
|
+ System.out.println(arr[0]);
|
|
|
+ return arr[0];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据经纬度转地址
|
|
|
+ * @param points
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String amapKey = "3609daa52e8ae4493393292213e2fb98";
|
|
|
+
|
|
|
+ //经度和纬度用","分割,经度在前,纬度在后,经纬度小数点后不得超过6位。多个坐标对之间用”|”进行分隔最多支持40对坐标。
|
|
|
+ public static AddressComponent pointsToLocationsAll(String points) {
|
|
|
+ //将GPS坐标转化为高德地图坐标的URL后再去请求位置信息
|
|
|
+ try {
|
|
|
+ points = URLEncoder.encode(points,"UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ String convertUrl =
|
|
|
+ "https://restapi.amap.com/v3/assistant/coordinate/convert?locations="+points+"&coordsys=gps&key="+amapKey;
|
|
|
+ //GPS坐标转为高德地图坐标
|
|
|
+ String s = HttpUtil.get(convertUrl);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(s);
|
|
|
+ String status = (String) jsonObject.get("status");
|
|
|
+ if(Objects.equals(status,"0")){
|
|
|
+ throw new RuntimeException("远程调用经纬度转化出错");
|
|
|
+ }
|
|
|
+ String locations = (String) jsonObject.get("locations");
|
|
|
+ String formattedAmapPoints = null;
|
|
|
+ try {
|
|
|
+ formattedAmapPoints = URLEncoder.encode(locations.replaceAll(";", "|"), "UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ String locationUrl = "https://restapi.amap.com/v3/geocode/regeo?output=json&location="+formattedAmapPoints+"&key="+amapKey+"&radius=1000&batch=true";
|
|
|
+ String s1 = HttpUtil.get(locationUrl);
|
|
|
+ System.out.println(s1);
|
|
|
+ //获取转地址后的结果
|
|
|
+ JSONObject parseObject = JSON.parseObject(s1);
|
|
|
+ String status1 = (String) parseObject.get("status");
|
|
|
+ if(Objects.equals(status1,"0")){
|
|
|
+ throw new RuntimeException("根据经纬度获取具体地址出错");
|
|
|
+ }
|
|
|
+ JSONArray regeocodes = parseObject.getJSONArray("regeocodes");
|
|
|
+ String formattedAddress = "";
|
|
|
+ AddressComponent addressComponent = null;
|
|
|
+ for (Object regeocode : regeocodes) {
|
|
|
+ JSONObject object = (JSONObject) regeocode;
|
|
|
+ formattedAddress = (String)object.get("formatted_address");
|
|
|
+ String addressComponentStr = (String)object.get("addressComponent");
|
|
|
+ addressComponent = JSONObject.parseObject(addressComponentStr, AddressComponent.class);
|
|
|
+ }
|
|
|
+ log.info("经纬度【{}】转化为具体地点【{}】",points,formattedAddress);
|
|
|
+ return addressComponent;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|