| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.utils.http.HttpUtils; |
| | | import com.ruoyi.common.config.TiandituMapConfig; |
| | | import com.ruoyi.system.service.IMapService; |
| | |
| | | return null; |
| | | } |
| | | |
| | | // 提取坐标 |
| | | JSONObject result = jsonObject.getJSONObject("result"); |
| | | if (result == null) { |
| | | logger.warn("天地图地理编码响应无result: address={}", address); |
| | | return null; |
| | | } |
| | | |
| | | JSONObject location = result.getJSONObject("location"); |
| | | |
| | | JSONObject location = jsonObject.getJSONObject("location"); |
| | | if (location == null) { |
| | | logger.warn("天地图地理编码响应无location: address={}", address); |
| | | return null; |
| | |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 直接返回地址 |
| | | * @param lng 经度 |
| | | * @param lat 纬度 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String reverseGeocoding(double lng, double lat) { |
| | | // 构建天地图逆地理编码API URL |
| | | String url = "http://api.tianditu.gov.cn/geocoder"; |
| | | String params = "postStr={\"lon\":" + lng + ",\"lat\":" + lat + ",\"ver\":1}" + |
| | | "&type=geocode" + |
| | | "&tk=" + tiandituMapConfig.getTk(); |
| | | |
| | | logger.info("天地图逆地理编码请求: lon={}, lat={}", lng, lat); |
| | | |
| | | // 发送HTTP请求 |
| | | String response = HttpUtils.sendGet(url, params); |
| | | |
| | | // "{\"msg\":\"ok\",\"location\":{\"score\":100,\"level\":\"门址\",\"lon\":\"116.290158\",\"lat\":\"39.894696\",\"keyWord\":\"北京市海淀区莲花池西路28号\"},\"searchVersion\":\"7.4.3V\",\"status\":\"0\"}" |
| | | logger.info("天地图逆地理编码响应: {}", response); |
| | | com.alibaba.fastjson.JSONObject obj= com.alibaba.fastjson.JSONObject.parseObject(response); |
| | | if (obj.getInteger("status") !=0) { |
| | | logger.error("天地图逆地理编码失败: {}", response); |
| | | return null; |
| | | } |
| | | com.alibaba.fastjson.JSONObject location = obj.getJSONObject("result"); |
| | | if (location == null) { |
| | | logger.error("天地图逆地理编码响应无location: {}", response); |
| | | return null; |
| | | } |
| | | return location.getString("formatted_address"); |
| | | |
| | | } |
| | | } |