wlzboy
2025-11-05 37de2f4b0f732ca5c19582d4a340ad7c987925b5
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/VehicleGpsController.java
@@ -575,6 +575,17 @@
            String geocodingResponse1 = HttpUtils.sendGet(geocodingUrl1, geocodingParams1);
            logger.info("起点地理编码响应: {}", geocodingResponse1);
            
            // 解析起点坐标
            com.alibaba.fastjson2.JSONObject geocodingJson1 = com.alibaba.fastjson2.JSONObject.parseObject(geocodingResponse1);
            if (geocodingJson1.getInteger("status") != 0) {
                logger.error("起点地理编码失败: {}", geocodingResponse1);
                return AjaxResult.error("起点地址解析失败");
            }
            com.alibaba.fastjson2.JSONObject location1 = geocodingJson1.getJSONObject("result").getJSONObject("location");
            double fromLat = location1.getDouble("lat");
            double fromLng = location1.getDouble("lng");
            logger.info("起点坐标: lat={}, lng={}", fromLat, fromLng);
            // 第二步:终点地址转坐标
            String geocodingUrl2 = "https://api.map.baidu.com/geocoding/v3/";
            String geocodingParams2 = "address=" + URLEncoder.encode(toAddress, StandardCharsets.UTF_8.toString()) +
@@ -586,14 +597,70 @@
            String geocodingResponse2 = HttpUtils.sendGet(geocodingUrl2, geocodingParams2);
            logger.info("终点地理编码响应: {}", geocodingResponse2);
            
            // 解析坐标(这里简化处理,实际应该解析JSON)
            // 注意:需要从响应中提取坐标,这里返回中间结果供前端处理
            Map<String, Object> result = new HashMap<>();
            result.put("fromGeocoding", geocodingResponse1);
            result.put("toGeocoding", geocodingResponse2);
            result.put("message", "请解析坐标后调用 /baidu/route/driving 接口计算距离");
            // 解析终点坐标
            com.alibaba.fastjson2.JSONObject geocodingJson2 = com.alibaba.fastjson2.JSONObject.parseObject(geocodingResponse2);
            if (geocodingJson2.getInteger("status") != 0) {
                logger.error("终点地理编码失败: {}", geocodingResponse2);
                return AjaxResult.error("终点地址解析失败");
            }
            com.alibaba.fastjson2.JSONObject location2 = geocodingJson2.getJSONObject("result").getJSONObject("location");
            double toLat = location2.getDouble("lat");
            double toLng = location2.getDouble("lng");
            logger.info("终点坐标: lat={}, lng={}", toLat, toLng);
            
            return AjaxResult.success("地理编码成功", result);
            // 第三步:调用路线规划接口计算距离
            String routeUrl = "https://api.map.baidu.com/directionlite/v1/driving";
            String origin = fromLat + "," + fromLng;
            String destination = toLat + "," + toLng;
            String routeParams = "origin=" + origin +
                                "&destination=" + destination +
                                "&ak=" + baiduMapConfig.getAk();
            logger.info("路线规划请求: origin={}, destination={}", origin, destination);
            String routeResponse = HttpUtils.sendGet(routeUrl, routeParams);
            logger.info("路线规划响应: {}", routeResponse);
            // 解析距离结果
            com.alibaba.fastjson2.JSONObject routeJson = com.alibaba.fastjson2.JSONObject.parseObject(routeResponse);
            if (routeJson.getInteger("status") != 0) {
                logger.error("路线规划失败: {}", routeResponse);
                return AjaxResult.error("路线规划失败");
            }
            // 提取距离信息(单位:米)
            com.alibaba.fastjson2.JSONObject result = routeJson.getJSONObject("result");
            com.alibaba.fastjson2.JSONArray routes = result.getJSONArray("routes");
            if (routes == null || routes.isEmpty()) {
                logger.error("未找到路线信息");
                return AjaxResult.error("未找到路线信息");
            }
            com.alibaba.fastjson2.JSONObject route = routes.getJSONObject(0);
            int distance = route.getInteger("distance"); // 距离,单位:米
            int duration = route.getInteger("duration"); // 时长,单位:秒
            logger.info("计算成功: 距离={}米, 时长={}秒", distance, duration);
            // 构建返回结果
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("distance", distance); // 距离(米)
            resultMap.put("duration", duration); // 时长(秒)
            resultMap.put("distanceKm", String.format("%.1f", distance / 1000.0)); // 距离(公里)
            resultMap.put("durationMin", duration / 60); // 时长(分钟)
            // 起点坐标
            Map<String, Object> fromLocation = new HashMap<>();
            fromLocation.put("lat", fromLat);
            fromLocation.put("lng", fromLng);
            resultMap.put("fromLocation", fromLocation);
            // 终点坐标
            Map<String, Object> toLocation = new HashMap<>();
            toLocation.put("lat", toLat);
            toLocation.put("lng", toLng);
            resultMap.put("toLocation", toLocation);
            return AjaxResult.success("计算成功", resultMap);
        } catch (Exception e) {
            logger.error("计算地址距离失败", e);
            return AjaxResult.error("计算距离失败:" + e.getMessage());