package com.xkrs.utils; import com.alibaba.fastjson.JSON; import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.controller.GaodeApiController; import com.xkrs.model.vo.GaoDeIgGeocodeVo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestBody; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.List; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; import static com.xkrs.utils.CommonConstant.INVERSEGEOGRAPHY_KEY; import static com.xkrs.utils.CommonConstant.INVERSEGEOGRAPHY_URL; import static com.xkrs.utils.HttpClientUtils.sendHttpsGet; /** * @author wudong * @date 2022/5/7 9:45 * @description */ public class GaoDeApiUtil { private static final Logger log = LoggerFactory.getLogger(GaoDeApiUtil.class); /** * 逆地理编码 * @param location 示例:["116,23", "117,24"] * @return */ public GaoDeIgGeocodeVo geocode(List location) { if (location.size() == 0) { return new GaoDeIgGeocodeVo(){{ setInfo("参数为空"); setInfocode("-1");}}; } StringBuilder sb = new StringBuilder(); for (String var : location) { sb.append(var).append("|"); } String url = INVERSEGEOGRAPHY_URL + "?key=" + INVERSEGEOGRAPHY_KEY + "&radius=1000&extensions=all&batch=true&roadlevel=0&location="; try { String encode = URLEncoder.encode(sb.toString(), StandardCharsets.UTF_8); String res = sendHttpsGet(url + encode); return JSON.parseObject(res, GaoDeIgGeocodeVo.class); } catch (Exception e) { log.error("获取高德天气失败, {}", e.getMessage()); } return new GaoDeIgGeocodeVo(){{ setInfo("获取高德天气失败"); setInfocode("-1"); }}; } }