From 3faea128cb8fb041c3164e2313a164d305660a62 Mon Sep 17 00:00:00 2001
From: lisy <linlangsur163@163.com>
Date: 星期一, 03 七月 2023 16:16:27 +0800
Subject: [PATCH] 探索玩湃:接口开发

---
 cloud-server-account/src/main/java/com/dsh/account/util/GDMapGeocodingUtil.java |   93 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 93 insertions(+), 0 deletions(-)

diff --git a/cloud-server-account/src/main/java/com/dsh/account/util/GDMapGeocodingUtil.java b/cloud-server-account/src/main/java/com/dsh/account/util/GDMapGeocodingUtil.java
index 1b136d5..c9d9dc7 100644
--- a/cloud-server-account/src/main/java/com/dsh/account/util/GDMapGeocodingUtil.java
+++ b/cloud-server-account/src/main/java/com/dsh/account/util/GDMapGeocodingUtil.java
@@ -8,6 +8,13 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -155,4 +162,90 @@
         }
         return map;
     }
+
+
+    /**
+     * 功能描述: 根据两个定位点的经纬度算出两点间的距离(米/m)
+     *
+     * @param startLonLat 起始经纬度
+     * @param endLonLat   结束经纬度(目标经纬度)
+     * @return java.lang.Long 两个定位点之间的距离
+     */
+    public Long getDistance(String startLonLat, String endLonLat) {
+        try {
+            // 返回起始地startAddr与目的地endAddr之间的距离,单位:米
+            Long result = new Long(0);
+            String queryUrl =
+                    "http://restapi.amap.com/v3/distance?key=" + 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());
+            return result;
+        } catch (Exception e) {
+            throw new RuntimeException();
+        }
+    }
+
+
+    /**
+     * 功能描述: 根据两个定位点的经纬度算出两点间的距离(千米/km)
+     *
+     * @param startLonLat 起始经纬度
+     * @param endLonLat   结束经纬度(目标经纬度)
+     * @return java.lang.Long 两个定位点之间的距离
+     */
+    public String getDistanceTOKilometer(String startLonLat, String endLonLat) {
+        try {
+            // 返回起始地startAddr与目的地endAddr之间的距离,单位:米
+            Long result = new Long(0);
+            String queryUrl =
+                    "http://restapi.amap.com/v3/distance?key=" + 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());
+            double kilDis = (double) result / 1000;
+            DecimalFormat decimalFormat = new DecimalFormat("#.00");
+            return decimalFormat.format(kilDis);
+        } catch (Exception e) {
+            throw new RuntimeException();
+        }
+    }
+
+    /**
+     * 功能描述: 发送请求
+     *
+     * @param serverUrl 请求地址
+     * @return java.lang.String
+     * @author isymikasan
+     * @date 2022-01-26 09:15:01
+     */
+    private static String getResponse(String serverUrl) {
+        // 用JAVA发起http请求,并返回json格式的结果
+        StringBuffer result = new StringBuffer();
+        try {
+            URL url = new URL(serverUrl);
+            URLConnection conn = url.openConnection();
+            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+            String line;
+            while ((line = in.readLine()) != null) {
+                result.append(line);
+            }
+            in.close();
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return result.toString();
+    }
+
+
 }

--
Gitblit v1.7.1