From c4ae4f10ab9b20a2d5a83bfad65929f394e31d2d Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Mon, 2 Dec 2024 15:17:46 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=86=E5=9C=B0=E7=90=86=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=A4=A9=E5=9C=B0=E5=9B=BE=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E9=AB=98=E5=BE=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/model/vo/TianDiTuGeocodeVo.java | 66 +++++++++++++++++++ .../service/impl/FirePointServiceImpl.java | 26 ++++---- .../java/com/xkrs/utils/TianDiTuApiUtil.java | 41 ++++++++++++ 3 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/xkrs/model/vo/TianDiTuGeocodeVo.java create mode 100644 src/main/java/com/xkrs/utils/TianDiTuApiUtil.java diff --git a/src/main/java/com/xkrs/model/vo/TianDiTuGeocodeVo.java b/src/main/java/com/xkrs/model/vo/TianDiTuGeocodeVo.java new file mode 100644 index 0000000..e195dce --- /dev/null +++ b/src/main/java/com/xkrs/model/vo/TianDiTuGeocodeVo.java @@ -0,0 +1,66 @@ +package com.xkrs.model.vo; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class TianDiTuGeocodeVo { + + @JsonProperty("result") + public ResultDTO result; + @JsonProperty("msg") + public String msg; + @JsonProperty("status") + public String status; + + public static class ResultDTO { + @JsonProperty("formatted_address") + public String formattedAddress; + @JsonProperty("location") + public LocationDTO location; + @JsonProperty("addressComponent") + public AddressComponentDTO addressComponent; + + public static class LocationDTO { + @JsonProperty("lon") + public Double lon; + @JsonProperty("lat") + public Double lat; + } + + public static class AddressComponentDTO { + @JsonProperty("address") + public String address; + @JsonProperty("town") + public String town; + @JsonProperty("nation") + public String nation; + @JsonProperty("city") + public String city; + @JsonProperty("county_code") + public String countyCode; + @JsonProperty("poi_position") + public String poiPosition; + @JsonProperty("county") + public String county; + @JsonProperty("city_code") + public String cityCode; + @JsonProperty("address_position") + public String addressPosition; + @JsonProperty("poi") + public String poi; + @JsonProperty("province_code") + public String provinceCode; + @JsonProperty("town_code") + public String townCode; + @JsonProperty("province") + public String province; + @JsonProperty("road") + public String road; + @JsonProperty("road_distance") + public Integer roadDistance; + @JsonProperty("address_distance") + public Integer addressDistance; + @JsonProperty("poi_distance") + public Integer poiDistance; + } + } +} diff --git a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java index aadab94..b518b04 100644 --- a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java @@ -6,7 +6,7 @@ import com.xkrs.model.entity.CountyCodeWeiXinEntity; import com.xkrs.model.entity.FirePointEntity; import com.xkrs.model.entity.ShanDongFirePointEntity; import com.xkrs.model.qo.FirePointQo; -import com.xkrs.model.vo.GaoDeIgGeocodeVo; +import com.xkrs.model.vo.TianDiTuGeocodeVo; import com.xkrs.service.FirePointService; import com.xkrs.service.GlobalConfigService; import com.xkrs.service.StreetService; @@ -166,19 +166,17 @@ public class FirePointServiceImpl implements FirePointService { * 为实体类绑定地址、编号、名称 */ private void bindAddress(FirePointEntity firePointEntity) { - List locationList = new ArrayList<>(); - locationList.add(firePointEntity.getLongitude() + "," + firePointEntity.getLatitude()); - GaoDeIgGeocodeVo geocode = GaoDeApiUtil.geocode(locationList); - GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0); - String formattedAddress = reGeoCode.getFormatted_address(); - firePointEntity.setFirePointAddress(formattedAddress); - GaoDeIgGeocodeVo.AddressComponent addressComponent = reGeoCode.getAddressComponent().get(0); - firePointEntity.setCountyCode(addressComponent.getAdcode()); - firePointEntity.setCountyName(addressComponent.getDistrict()); - String townCode = addressComponent.getTowncode(); - String fixedTownCode = townCode.length() > 9 ? townCode.substring(0, 9) : townCode; - firePointEntity.setStreetCode(fixedTownCode); - firePointEntity.setStreetName(addressComponent.getTownship()); + try { + TianDiTuGeocodeVo geocode = TianDiTuApiUtil.geocode(firePointEntity.getLongitude(), firePointEntity.getLatitude()); + TianDiTuGeocodeVo.ResultDTO.AddressComponentDTO addressComponent = geocode.result.addressComponent; + firePointEntity.setFirePointAddress(geocode.result.formattedAddress); + firePointEntity.setCountyCode(addressComponent.countyCode.substring(3, 9)); + firePointEntity.setCountyName(addressComponent.county); + firePointEntity.setStreetCode(addressComponent.townCode.substring(3, 12)); + firePointEntity.setStreetName(addressComponent.town); + } catch (Exception e) { + e.printStackTrace(); + } } /** diff --git a/src/main/java/com/xkrs/utils/TianDiTuApiUtil.java b/src/main/java/com/xkrs/utils/TianDiTuApiUtil.java new file mode 100644 index 0000000..13d7594 --- /dev/null +++ b/src/main/java/com/xkrs/utils/TianDiTuApiUtil.java @@ -0,0 +1,41 @@ +package com.xkrs.utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.xkrs.model.vo.TianDiTuGeocodeVo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Map; + +public class TianDiTuApiUtil { + + private static final Logger log = LoggerFactory.getLogger(TianDiTuApiUtil.class); + + public static TianDiTuGeocodeVo geocode(double longitude, double latitude) { + + try { + + Map jsonObject = new HashMap<>(); + jsonObject.put("lon", longitude); + jsonObject.put("lat", latitude); + jsonObject.put("ver", 1); + String jsonParams = new JSONObject(jsonObject).toString(); + String encodedParams = URLEncoder.encode(jsonParams, "UTF-8"); + String url = "http://api.tianditu.gov.cn/geocoder?postStr=" + encodedParams + "&type=geocode&tk=e16fa4bdf9a5f59195239ea28622e3d5"; + return JSON.parseObject(HttpClientUtils.sendHttpGet(url), TianDiTuGeocodeVo.class); + + } catch (Exception e) { + e.printStackTrace(); + } + return null; + + } + + public static void main(String[] args) { + TianDiTuGeocodeVo geocode = geocode(110.89, 36.78); + } + +}