逆地理编码使用天地图替换高德

This commit is contained in:
liuchengqian 2024-12-02 14:25:52 +08:00
parent 689aea259f
commit ca2b4684d9
3 changed files with 122 additions and 22 deletions

View File

@ -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;
}
}
}

View File

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

View File

@ -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<String, Object> 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);
}
}