81 lines
3.1 KiB
Java
81 lines
3.1 KiB
Java
package com.xkrs.utilsnew;
|
|
|
|
import com.xkrs.model.bean.AddressBean;
|
|
import com.xkrs.model.vo.TianDiTuGeocodeVo;
|
|
import org.apache.hc.core5.util.TextUtils;
|
|
|
|
public class FirePointAddressUtils {
|
|
|
|
private FirePointAddressUtils() {
|
|
}
|
|
|
|
/**
|
|
* 使用网络方式解析地址信息
|
|
*/
|
|
public static AddressBean analysisWithNetwork(double longitude, double latitude) {
|
|
try {
|
|
TianDiTuGeocodeVo geocode = TianDiTuApiUtil.geocode(longitude, latitude);
|
|
TianDiTuGeocodeVo.ResultDTO.AddressComponentDTO addressComponent = geocode.result.addressComponent;
|
|
//填充AddressBean数据
|
|
AddressBean addressBean = new AddressBean();
|
|
addressBean.setSuccess(true);
|
|
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;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
AddressBean addressBean = new AddressBean();
|
|
addressBean.setSuccess(false);
|
|
return addressBean;
|
|
}
|
|
|
|
/**
|
|
* 检查AddressBean的合法性
|
|
*/
|
|
private static void checkValidAddress(AddressBean addressBean) {
|
|
if (addressBean.getProCode().length() != 6) {
|
|
throw new RuntimeException("ProCode Error");
|
|
}
|
|
if (TextUtils.isEmpty(addressBean.getProName())) {
|
|
throw new RuntimeException("ProName Error");
|
|
}
|
|
if (addressBean.getCityCode().length() != 6) {
|
|
throw new RuntimeException("CityCode Error");
|
|
}
|
|
if (TextUtils.isEmpty(addressBean.getCityName())) {
|
|
throw new RuntimeException("CityName Error");
|
|
}
|
|
if (addressBean.getCountyCode().length() != 6) {
|
|
throw new RuntimeException("CountyCode Error");
|
|
}
|
|
if (TextUtils.isEmpty(addressBean.getCountyName())) {
|
|
throw new RuntimeException("CountyName Error");
|
|
}
|
|
if (addressBean.getTownCode().length() != 9) {
|
|
throw new RuntimeException("TownCode Error");
|
|
}
|
|
if (TextUtils.isEmpty(addressBean.getTownName())) {
|
|
throw new RuntimeException("TownName Error");
|
|
}
|
|
if (TextUtils.isEmpty(addressBean.getFirePointAddress())) {
|
|
throw new RuntimeException("FirePointAddress Error");
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
AddressBean addressBean = analysisWithNetwork(120.98, 36.78);
|
|
System.out.println(addressBean.toString());
|
|
}
|
|
|
|
}
|