| | |
| | | @GetMapping("/tianditu/place/suggestion") |
| | | public AjaxResult tiandituPlaceSuggestion(String keyWord, String region, String city, Integer count) { |
| | | try { |
| | | // 检查参数 |
| | | // 检查必需参数 |
| | | if (keyWord == null || keyWord.trim().isEmpty()) { |
| | | return AjaxResult.error("参数不完整,缺少搜索关键词"); |
| | | return AjaxResult.error("参数不完整,缺少搜索关键词(keyWord)"); |
| | | } |
| | | |
| | | // 设置默认值 |
| | |
| | | count = 10; |
| | | } |
| | | |
| | | // 构建天地图输入提示API URL |
| | | String url = "http://api.tianditu.gov.cn/search"; |
| | | // 构建天地图普通搜索API URL |
| | | String url = "http://api.tianditu.gov.cn/v2/search"; |
| | | StringBuilder paramsBuilder = new StringBuilder(); |
| | | paramsBuilder.append("postStr={\"keyWord\":\"").append(keyWord).append("\""); |
| | | // 硬编码中国地图范围 |
| | | paramsBuilder.append(",\"mapBound\":\"").append("73.66,3.86,135.05,53.55").append("\""); |
| | | // 默认级别为18 |
| | | paramsBuilder.append(",\"level\":\"").append("18").append("\""); |
| | | // 默认查询类型为普通搜索 |
| | | paramsBuilder.append(",\"queryType\":\"").append("1").append("\""); |
| | | // 默认起始位置为0 |
| | | paramsBuilder.append(",\"start\":\"").append("0").append("\""); |
| | | paramsBuilder.append(",\"count\":\"").append(count).append("\""); |
| | | |
| | | // 添加可选参数 |
| | | if (region != null && !region.trim().isEmpty()) { |
| | | paramsBuilder.append(",\"region\":\"").append(region).append("\""); |
| | | paramsBuilder.append(",\"specify\":\"").append(region).append("\""); |
| | | } |
| | | if (city != null && !city.trim().isEmpty()) { |
| | | paramsBuilder.append(",\"city\":\"").append(city).append("\""); |
| | | paramsBuilder.append(",\"dataTypes\":\"").append(city).append("\""); |
| | | } |
| | | paramsBuilder.append(",\"count\":\"").append(count).append("\""); |
| | | paramsBuilder.append("}" ); |
| | | paramsBuilder.append("&type=suggest"); |
| | | |
| | | paramsBuilder.append("}"); |
| | | paramsBuilder.append("&type=query"); |
| | | paramsBuilder.append("&tk=").append(tiandituMapConfig.getTk()); |
| | | |
| | | String params = paramsBuilder.toString(); |
| | | |
| | | logger.info("天地图输入提示请求: keyWord={}, region={}", keyWord, region); |
| | | logger.info("天地图普通搜索请求: keyWord={}, count={}", keyWord, count); |
| | | logger.debug("地图请求接口参数:{}", params); |
| | | |
| | | // 发送HTTP请求 |
| | | String response = HttpUtils.sendGet(url, params); |
| | | logger.debug("天地图输入提示响应: {}", response); |
| | | logger.debug("天地图普通搜索响应: {}", response); |
| | | |
| | | // 解析响应 |
| | | com.alibaba.fastjson2.JSONObject jsonResponse = com.alibaba.fastjson2.JSONObject.parseObject(response); |
| | | if (!"0".equals(jsonResponse.getString("status"))) { |
| | | logger.error("输入提示失败: {}", response); |
| | | logger.error("普通搜索失败: {}", response); |
| | | return AjaxResult.error("地址搜索失败"); |
| | | } |
| | | |
| | | // 提取提示列表 |
| | | com.alibaba.fastjson2.JSONArray results = jsonResponse.getJSONArray("suggests"); |
| | | // 提取搜索结果 |
| | | com.alibaba.fastjson2.JSONArray results = jsonResponse.getJSONArray("pois"); |
| | | if (results == null || results.isEmpty()) { |
| | | logger.info("未找到匹配的地址"); |
| | | return AjaxResult.success("查询成功", new ArrayList<>()); |
| | |
| | | |
| | | 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("address", item.getString("addr")); // 地址 |
| | | suggestion.put("province", item.getString("prov")); // 省 |
| | | suggestion.put("city", item.getString("city")); // 市 |
| | | suggestion.put("district", item.getString("district")); // 区 |
| | | suggestion.put("district", item.getString("county")); // 区县 |
| | | suggestion.put("tel", item.getString("tel")); // 电话 |
| | | suggestion.put("postcode", item.getString("postcode")); // 邮编 |
| | | suggestion.put("type", item.getString("type")); // 类型 |
| | | |
| | | // 经纬度信息 |
| | | com.alibaba.fastjson2.JSONObject location = item.getJSONObject("location"); |
| | | com.alibaba.fastjson2.JSONObject location = item.getJSONObject("lonlat"); |
| | | if (location != null) { |
| | | Map<String, Object> locationMap = new HashMap<>(); |
| | | locationMap.put("lon", location.getDouble("lon")); |
| | |
| | | suggestions.add(suggestion); |
| | | } |
| | | |
| | | logger.info("地址搜索提示成功: 找到{}条结果", suggestions.size()); |
| | | logger.info("地址搜索成功: 找到{}条结果", suggestions.size()); |
| | | return AjaxResult.success("查询成功", suggestions); |
| | | } catch (Exception e) { |
| | | logger.error("地址搜索提示失败", e); |
| | | logger.error("地址搜索失败", e); |
| | | return AjaxResult.error("地址搜索失败:" + e.getMessage()); |
| | | } |
| | | } |