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

This commit is contained in:
liuchengqian 2024-12-02 15:17:46 +08:00
parent 3d9286ed3b
commit c4ae4f10ab
3 changed files with 119 additions and 14 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

@ -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<String> 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();
}
}
/**

View File

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