From 02bb94e413f6950b9786c5ee86c0937bc20f8ae8 Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期六, 12 七月 2025 14:42:20 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- ruoyi-admin/src/main/java/com/ruoyi/web/util/AmapApiClient.java | 36 +++++++++++++++++++++++++++++++++++- 1 files changed, 35 insertions(+), 1 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/util/AmapApiClient.java b/ruoyi-admin/src/main/java/com/ruoyi/web/util/AmapApiClient.java index 3ea9c5c..30cb842 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/util/AmapApiClient.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/util/AmapApiClient.java @@ -1,17 +1,24 @@ package com.ruoyi.web.util; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import org.springframework.stereotype.Component; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +@Component public class AmapApiClient { private static final String KEY = "e700a58329a38b2d8980790d9b1b5b06"; private static final OkHttpClient client = new OkHttpClient(); public static String getDrivingRoute(double startLat, double startLon, double endLat, double endLon) throws IOException { - String url = "https://restapi.amap.com/v3/direction/walking?" + + String url = "https://restapi.amap.com/v3/direction/driving?" + "origin=" + startLon + "," + startLat + "&destination=" + endLon + "," + endLat + "&key=" + KEY; @@ -24,4 +31,31 @@ return response.body().string(); } } + public static Map<String, String> getDistance(String origins, String destination, Integer type) throws IOException { + String url = "https://restapi.amap.com/v3/distance?key=" + KEY + "&origins=" + origins + "&destination=" + destination + + "&type=" + type; + + Request request = new Request.Builder() + .url(url) + .build(); + + try (Response response = client.newCall(request).execute()) { + JSONObject jsonObject = JSON.parseObject(response.body().string()); + String status = jsonObject.getString("status"); + +// gdInterfaceService.saveData("https://restapi.amap.com/v3/distance", "查询两点间的距离"); + + if(status.equals("1")){ + JSONArray results = jsonObject.getJSONArray("results"); + JSONObject jsonObject1 = results.getJSONObject(0); + Map<String, String> map = new HashMap<>(); + map.put("distance", jsonObject1.getString("distance"));//距离(米) + map.put("duration", jsonObject1.getString("duration"));//预计时间(秒) + return map; + }else{ + System.err.println(jsonObject); + } + } + return null; + } } -- Gitblit v1.7.1