yq 3 rokov pred
rodič
commit
da2f7c3b18

+ 33 - 0
mhfire-controller/src/main/java/com/bizmatics/mhfire/controller/Test.java

@@ -0,0 +1,33 @@
+package com.bizmatics.mhfire.controller;
+
+import com.bizmatics.mhfire.service.util.GouldUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @author yq
+ * @date 2021/7/23 16:22
+ */
+@RestController
+@RequestMapping("test")
+public class Test {
+
+    @Autowired
+    private GouldUtil gouldUtil;
+
+
+    @GetMapping("/getAddress")
+    public String getAddress(String longitude,String lat) {
+        System.out.println(longitude);
+        System.out.println(lat);
+        String address = gouldUtil.getAMapByLngAndLat(longitude, lat);
+        return address;
+    }
+
+    @GetMapping("/getLonLat")
+    public String getLonLat(@RequestParam String address) {
+        String result = gouldUtil.getLonLat(address);
+        return result;
+    }
+
+}

+ 1 - 0
mhfire-controller/src/main/resources/application-dev.properties

@@ -66,6 +66,7 @@ spring.jackson.parser.allow-single-quotes=true
 server.compression.enabled=true
 server.compression.mime-types=application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain
 
+gould.Key=b4414402710da7c5fcbb775ec7b83408
 
 # eureka
 #eureka.client.service-url.defaultZone=http://172.31.101.251:8099/eureka/,http://172.31.101.252:8099/eureka/

+ 1 - 0
mhfire-controller/src/main/resources/application-prod.properties

@@ -62,6 +62,7 @@ spring.jackson.parser.allow-single-quotes=true
 server.compression.enabled=true
 server.compression.mime-types=application/javascript,text/css,application/json,application/xml,text/html,text/xml,text/plain
 
+gould.Key=8e266e1ac2ad2383c7773ff504ac248f
 
 # eureka
 #eureka.client.service-url.defaultZone=http://172.31.101.251:8099/eureka/,http://172.31.101.252:8099/eureka/

+ 134 - 0
mhfire-service/src/main/java/com/bizmatics/mhfire/service/util/GouldUtil.java

@@ -0,0 +1,134 @@
+package com.bizmatics.mhfire.service.util;
+
+import com.bizmatics.common.core.util.HttpUtils;
+import com.bizmatics.common.spring.util.JsonUtils;
+import com.fasterxml.jackson.core.type.TypeReference;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * @author yq
+ * @date 2021/7/23 16:10
+ */
+@Component
+public class GouldUtil {
+
+    @Value("${gould.Key}")
+    private String gouldKey;
+
+
+
+    //申请的账户Key
+
+    /**
+     * 0.根据地址名称得到两个地址间的距离
+     * @param start 起始位置
+     * @param start 结束位置
+     * @return 两个地址间的距离
+     */
+    public long getDistanceByAddress(String start, String end) {
+        String startLonLat = getLonLat(start);
+        String endLonLat = getLonLat(end);
+        long dis = getDistance(startLonLat, endLonLat);
+        return dis;
+    }
+
+    /**
+     * 1.地址转换为经纬度
+     * @param address 地址
+     * @return 经纬度
+     */
+    public String getLonLat(String address) {
+        // 返回输入地址address的经纬度信息, 格式是 经度,纬度
+        String queryUrl = "http://restapi.amap.com/v3/geocode/geo?key="+gouldKey+"&address=" + address;
+        String queryResult = getResponse(queryUrl); // 高德接品返回的是JSON格式的字符串
+        Map<String, Object> result = JsonUtils.fromJson(queryResult, new TypeReference<Map<String, Object>>() {});
+        return result.get("location").toString();
+    }
+
+    /**
+     * 将经纬度getLng, getLat 通过getAMapByLngAndLat方法转换地址
+     * @param getLng 经度
+     * @param getLat 纬度
+     * @return 地址名称
+     * @throws Exception
+     */
+    public String getAMapByLngAndLat(String getLng, String getLat){
+//        String url;
+//        try {
+//            url = "http://restapi.amap.com/v3/geocode/regeo?output=JSON&location=" + getLng + "," + getLat
+//                    + "&key="+GOULD_KEY+"&radius=0&extensions=base";
+//            System.out.println(getLat
+//            );
+//            System.out.println(url);
+//            String queryResult = getResponse(url); // 高德接品返回的是JSON格式的字符串
+//            if (queryResult == null) {
+//                return "-1";
+//            }
+//            // 将获取结果转为json 数据
+//            JSONObject obj = JSONObject.parseObject(queryResult);
+//            if (obj.get("status").toString().equals("1")) {
+//                // 如果没有返回-1
+//                JSONObject regeocode = obj.getJSONObject("regeocode");
+//                if (regeocode.size() > 0) {
+//                    // 在regeocode中拿到 formatted_address 具体位置
+//                    String formatted = regeocode.get("formatted_address").toString();
+//                    return formatted;
+//
+//                } else {
+//                    System.out.println("未找到相匹配的地址!");
+//                    return "-1";
+//
+//                }
+//            } else {
+//                System.out.println("请求错误!");
+//                return "-1";
+//            }
+//        } catch (Exception e) {
+//            // TODO Auto-generated catch block
+//            e.printStackTrace();
+//        }
+//        return "-1";
+        return null;
+    }
+
+    /**
+     * 2.根据两个定位点的经纬度算出两点间的距离
+     * @param startLonLat 起始经纬度
+     * @param endLonLat 结束经纬度(目标经纬度)
+     * @return 两个定位点之间的距离
+     */
+    private long getDistance(String startLonLat, String endLonLat) {
+        // 返回起始地startAddr与目的地endAddr之间的距离,单位:米
+//        Long result = new Long(0);
+//        String queryUrl = "http://restapi.amap.com/v3/distance?key="+GOULD_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());
+////		System.out.println("距离2:" + result);
+//        return result;
+        return 1;
+    }
+
+    /**
+     * 3.发送请求
+     * @param serverUrl 请求地址
+     */
+    private static String getResponse(String serverUrl) {
+        // 用JAVA发起http请求,并返回json格式的结果
+        String result = "";
+        try {
+            result = HttpUtils.get(serverUrl, null);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return result;
+    }
+
+}