| | |
| | | return AjaxResult.error("计算距离失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 百度地图地址搜索提示API(输入联想) |
| | | * Place Suggestion API - 用于地址输入时的智能提示 |
| | | */ |
| | | @Anonymous() |
| | | @GetMapping("/baidu/place/suggestion") |
| | | public AjaxResult baiduPlaceSuggestion(String query, String region) { |
| | | try { |
| | | // 检查参数 |
| | | if (query == null || query.trim().isEmpty()) { |
| | | return AjaxResult.error("参数不完整,缺少搜索关键词"); |
| | | } |
| | | |
| | | // 构建百度地图 Place Suggestion API URL |
| | | String url = "https://api.map.baidu.com/place/v2/suggestion"; |
| | | String params = "query=" + URLEncoder.encode(query, StandardCharsets.UTF_8.toString()) + |
| | | (region != null && !region.trim().isEmpty() ? |
| | | "®ion=" + URLEncoder.encode(region, StandardCharsets.UTF_8.toString()) : "") + |
| | | "&output=json" + |
| | | "&ak=" + baiduMapConfig.getAk(); |
| | | |
| | | logger.info("百度地图地址搜索提示请求: query={}, region={}", query, region); |
| | | |
| | | // 发送HTTP请求 |
| | | String response = HttpUtils.sendGet(url, params); |
| | | logger.debug("百度地图地址搜索提示响应: {}", response); |
| | | |
| | | // 解析响应 |
| | | com.alibaba.fastjson2.JSONObject jsonResponse = com.alibaba.fastjson2.JSONObject.parseObject(response); |
| | | if (jsonResponse.getInteger("status") != 0) { |
| | | logger.error("地址搜索提示失败: {}", response); |
| | | return AjaxResult.error("地址搜索失败"); |
| | | } |
| | | |
| | | // 提取提示列表 |
| | | com.alibaba.fastjson2.JSONArray results = jsonResponse.getJSONArray("result"); |
| | | if (results == null || results.isEmpty()) { |
| | | logger.info("未找到匹配的地址"); |
| | | return AjaxResult.success("查询成功", new ArrayList<>()); |
| | | } |
| | | |
| | | // 构建返回结果 |
| | | List<Map<String, Object>> suggestions = new ArrayList<>(); |
| | | for (int i = 0; i < results.size(); i++) { |
| | | com.alibaba.fastjson2.JSONObject item = results.getJSONObject(i); |
| | | |
| | | Map<String, Object> suggestion = new HashMap<>(); |
| | | suggestion.put("name", item.getString("name")); // 名称 |
| | | suggestion.put("address", item.getString("address")); // 地址 |
| | | suggestion.put("province", item.getString("province")); // 省 |
| | | suggestion.put("city", item.getString("city")); // 市 |
| | | suggestion.put("district", item.getString("district")); // 区 |
| | | suggestion.put("uid", item.getString("uid")); // 地点UID |
| | | |
| | | // 经纬度信息 |
| | | com.alibaba.fastjson2.JSONObject location = item.getJSONObject("location"); |
| | | if (location != null) { |
| | | Map<String, Object> locationMap = new HashMap<>(); |
| | | locationMap.put("lat", location.getDouble("lat")); |
| | | locationMap.put("lng", location.getDouble("lng")); |
| | | suggestion.put("location", locationMap); |
| | | } |
| | | |
| | | suggestions.add(suggestion); |
| | | } |
| | | |
| | | logger.info("地址搜索提示成功: 找到{} 条结果", suggestions.size()); |
| | | return AjaxResult.success("查询成功", suggestions); |
| | | } catch (Exception e) { |
| | | logger.error("地址搜索提示失败", e); |
| | | return AjaxResult.error("地址搜索失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | } |