com.liuchink.chinkutils.baidu.ChinkBaiDuMapGeocoding3Utils Maven / Gradle / Ivy
package com.liuchink.chinkutils.baidu;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.liuchink.chinkutils.baidu.bean.geocoding.*;
import com.liuchink.chinkutils.json.ChinkFastJsonUtils;
import com.liuchink.chinkutils.net.ChinkOkHttpUtils;
import com.liuchink.chinkutils.net.bean.HttpResponse;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;
/**
* 地理编码V3.0
*
* @author liugang
* @since 2020/10/27 15:40
*/
@Slf4j
public class ChinkBaiDuMapGeocoding3Utils {
private ChinkBaiDuMapGeocoding3Utils() {
}
/**
* 地理编码解析
*
* @param geocodingReq
* geocodingReq
* @return com.liuchink.chinkutils.baidu.map.bean.BaiDuMapResp
* @author liugang
* @since 2020/10/27 15:49
*/
public static GeocodingResp geocoding(
GeocodingReq geocodingReq) throws NoSuchAlgorithmException,
IOException, KeyManagementException {
StringBuilder url = new StringBuilder(
"https://api.map.baidu.com/geocoding/v3/?");
for (Map.Entry stringEntry : JSON
.parseObject(ChinkFastJsonUtils.toJsonString(geocodingReq))
.entrySet()) {
if (StringUtils.isNotBlank(Convert.toStr(stringEntry.getValue()))) {
url.append(stringEntry.getKey()).append("=")
.append(stringEntry.getValue()).append("&");
}
}
String postUrl = CharSequenceUtil.removeSuffix(url, "&");
log.debug(":请求报文:【{}】", postUrl);
HttpResponse httpResponse = ChinkOkHttpUtils.doHttpGet(postUrl);
GeocodingResp geocodingResp = new GeocodingResp<>();
log.debug(":应答报文:【{}】",
httpResponse.getResult().getBody());
if (httpResponse.getStatus().getCode() == 200) {
if ("json".equals(geocodingReq.getOutput())) {
geocodingResp = JSON.parseObject(
httpResponse.getResult().getBody(),
new TypeReference>() {
});
} else {
geocodingResp.setXmlResult(httpResponse.getResult().getBody());
}
} else {
geocodingResp.setStatus(httpResponse.getStatus().getCode());
geocodingResp.setMessage("网络请求错误");
}
return geocodingResp;
}
/**
* 逆地理编码解析
*
* @param reverseGeocodingReq
* reverseGeocodingReq
* @return com.liuchink.chinkutils.baidu.map.bean.BaiDuMapRespons
* @author liugang
* @since 2020/10/27 16:08
*/
public static ReverseGeocodingResp reverGeocoding(
ReverseGeocodingReq reverseGeocodingReq)
throws NoSuchAlgorithmException, IOException,
KeyManagementException {
StringBuilder url = new StringBuilder(
"https://api.map.baidu.com/reverse_geocoding/v3/?");
for (Map.Entry stringEntry : JSON
.parseObject(
ChinkFastJsonUtils.toJsonString(reverseGeocodingReq))
.entrySet()) {
if (StringUtils.isNotBlank(Convert.toStr(stringEntry.getValue()))) {
url.append(stringEntry.getKey()).append("=")
.append(stringEntry.getValue()).append("&");
}
}
String postUrl = CharSequenceUtil.removeSuffix(url, "&");
log.debug(":请求报文:【{}】", postUrl);
HttpResponse httpResponse = ChinkOkHttpUtils.doHttpGet(postUrl);
ReverseGeocodingResp reverseGeocodingResp = new ReverseGeocodingResp<>();
log.debug(":应答报文:【{}】",
httpResponse.getResult().getBody());
if (httpResponse.getStatus().getCode() == 200) {
if ("json".equals(reverseGeocodingReq.getOutput())) {
reverseGeocodingResp = JSON.parseObject(
httpResponse.getResult().getBody(),
new TypeReference>() {
});
} else {
reverseGeocodingResp
.setXmlResult(httpResponse.getResult().getBody());
}
} else {
reverseGeocodingResp.setStatus(httpResponse.getStatus().getCode());
reverseGeocodingResp.setMessage("网络请求错误");
}
return reverseGeocodingResp;
}
}