| | |
| | | * 根据经纬度获取区县代码 |
| | | * @param longitude 经度 |
| | | * @param latitude 纬度 |
| | | * @return 区县代码 |
| | | * @return 区县代码(已去除156国家代码前缀) |
| | | */ |
| | | public static String getCountyCode(Double longitude, Double latitude) { |
| | | if (longitude == null || latitude == null) { |
| | |
| | | try { |
| | | String postStr = String.format("{'lon':%s,'lat':%s,'ver':1}", longitude, latitude); |
| | | String countyCode = requestCountyCode(postStr); |
| | | return StringUtil.isBlank(countyCode) ? null : countyCode; |
| | | // 去除156国家代码前缀 |
| | | return StringUtil.isBlank(countyCode) ? null : removeCountryCodePrefix(countyCode); |
| | | } catch (Exception e) { |
| | | logger.warn("天地图逆地理编码获取区县代码失败", e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据经纬度获取街道代码 |
| | | * @param longitude 经度 |
| | | * @param latitude 纬度 |
| | | * @return 街道代码(已去除156国家代码前缀) |
| | | */ |
| | | public static String getTownCode(Double longitude, Double latitude) { |
| | | if (longitude == null || latitude == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | String postStr = String.format("{'lon':%s,'lat':%s,'ver':1}", longitude, latitude); |
| | | String townCode = requestTownCode(postStr); |
| | | // 去除156国家代码前缀 |
| | | return StringUtil.isBlank(townCode) ? null : removeCountryCodePrefix(townCode); |
| | | } catch (Exception e) { |
| | | logger.warn("天地图逆地理编码获取街道代码失败", e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 去除行政区划代码中的156国家代码前缀 |
| | | * @param code 原始行政区划代码 |
| | | * @return 去除156前缀后的代码 |
| | | */ |
| | | private static String removeCountryCodePrefix(String code) { |
| | | if (StringUtil.isBlank(code)) { |
| | | return code; |
| | | } |
| | | // 156是中国的国家代码,去除前缀 |
| | | return code.startsWith("156") ? code.substring(3) : code; |
| | | } |
| | | |
| | | private static String requestAddress(String postStr) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 请求天地图API获取街道代码 |
| | | * @param postStr 请求参数 |
| | | * @return 街道代码 |
| | | */ |
| | | private static String requestTownCode(String postStr) { |
| | | // 步骤1:构建请求URL |
| | | String encodedPostStr = UriUtils.encodeQueryParam(postStr, StandardCharsets.UTF_8); |
| | | String url = TIAN_DI_TU_URL + "?postStr=" + encodedPostStr + "&type=geocode&tk=" + TIAN_DI_TU_KEY; |
| | | URI uri = URI.create(url); |
| | | |
| | | // 步骤2:发送HTTP请求 |
| | | String response = REST_TEMPLATE.getForObject(uri, String.class); |
| | | if (StringUtil.isBlank(response)) { |
| | | return null; |
| | | } |
| | | |
| | | // 步骤3:解析JSON响应 |
| | | try { |
| | | log.info("天地图逆地理编码结果:{}", response); |
| | | JsonNode root = OBJECT_MAPPER.readTree(response); |
| | | |
| | | // 步骤4:提取街道代码 |
| | | String townCode = root.path("result").path("addressComponent").path("town_code").asText(); |
| | | return StringUtil.isBlank(townCode) ? null : townCode; |
| | | } catch (Exception e) { |
| | | logger.warn("解析天地图逆地理编码街道代码失败", e); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | // public static void main(String[] args) { |
| | | // String formattedAddress = getFormattedAddress(116.39742, 39.90923); |
| | | // String countyCode = getCountyCode(116.39742, 39.90923); |