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/utilsnew/FirePointAddressUtils.java b/src/main/java/com/xkrs/utilsnew/FirePointAddressUtils.java index 4a95661..3479bdb 100644 --- a/src/main/java/com/xkrs/utilsnew/FirePointAddressUtils.java +++ b/src/main/java/com/xkrs/utilsnew/FirePointAddressUtils.java @@ -1,13 +1,9 @@ package com.xkrs.utilsnew; import com.xkrs.model.bean.AddressBean; -import com.xkrs.model.vo.GaoDeIgGeocodeVo; -import com.xkrs.utilsold.GaoDeApiUtil; +import com.xkrs.model.vo.TianDiTuGeocodeVo; import org.apache.hc.core5.util.TextUtils; -import java.util.ArrayList; -import java.util.List; - public class FirePointAddressUtils { private FirePointAddressUtils() { @@ -18,24 +14,20 @@ public class FirePointAddressUtils { */ public static AddressBean analysisWithNetwork(double longitude, double latitude) { try { - List locationList = new ArrayList<>(); - locationList.add(longitude + "," + latitude); - GaoDeIgGeocodeVo geocode = GaoDeApiUtil.geocode(locationList); - GaoDeIgGeocodeVo.Regeocode reGeoCode = geocode.getRegeocodes().get(0); - GaoDeIgGeocodeVo.AddressComponent addressComponent = reGeoCode.getAddressComponent().get(0); - String townCode = addressComponent.getTowncode(); + TianDiTuGeocodeVo geocode = TianDiTuApiUtil.geocode(longitude, latitude); + TianDiTuGeocodeVo.ResultDTO.AddressComponentDTO addressComponent = geocode.result.addressComponent; //填充AddressBean数据 AddressBean addressBean = new AddressBean(); addressBean.setSuccess(true); - addressBean.setProCode(townCode.substring(0, 2) + "0000"); - addressBean.setProName(addressComponent.getProvince()); - addressBean.setCityCode(townCode.substring(0, 4) + "00"); - addressBean.setCityName(addressComponent.getCity()); - addressBean.setCountyCode(townCode.substring(0, 6)); - addressBean.setCountyName(addressComponent.getDistrict()); - addressBean.setTownCode(townCode.substring(0, 9)); - addressBean.setTownName(addressComponent.getTownship()); - addressBean.setFirePointAddress(reGeoCode.getFormatted_address()); + addressBean.setProCode(addressComponent.provinceCode.substring(3, 9)); + addressBean.setProName(addressComponent.province); + addressBean.setCityCode(addressComponent.cityCode.substring(3, 9)); + addressBean.setCityName(addressComponent.city); + addressBean.setCountyCode(addressComponent.countyCode.substring(3, 9)); + addressBean.setCountyName(addressComponent.county); + addressBean.setTownCode(addressComponent.townCode.substring(3, 12)); + addressBean.setTownName(addressComponent.town); + addressBean.setFirePointAddress(geocode.result.formattedAddress); //检查AddressBean的合法性 checkValidAddress(addressBean); return addressBean; @@ -81,8 +73,8 @@ public class FirePointAddressUtils { } public static void main(String[] args) { - - AddressBean addressBean = analysisWithNetwork(120, 36); + AddressBean addressBean = analysisWithNetwork(120.98, 36.78); System.out.println(addressBean.toString()); } + } diff --git a/src/main/java/com/xkrs/utilsnew/TianDiTuApiUtil.java b/src/main/java/com/xkrs/utilsnew/TianDiTuApiUtil.java new file mode 100644 index 0000000..2569ba7 --- /dev/null +++ b/src/main/java/com/xkrs/utilsnew/TianDiTuApiUtil.java @@ -0,0 +1,42 @@ +package com.xkrs.utilsnew; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.xkrs.model.vo.TianDiTuGeocodeVo; +import com.xkrs.utilsold.HttpClientUtils; +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); + } + +}