package com.xiaopeng.itinerary.utils.map; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.xiaopeng.common.core.utils.HttpUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * 高德地图的地理编码工具类 */ @Component @Lazy public class GDMapGeocodingUtil { @Value("${map.api-key}") private String key; @Value("${map.urls.reverse-geocode}") private String reverseGeocodeUrl; /** * 根据经纬度获取行政区域信息 * @param lon * @param lan * @return * @throws Exception */ public Map geocode(String lon, String lan) { String url = reverseGeocodeUrl + "?key=" + key + "&location=" + lon + "," + lan; String forObject = HttpUtils.sendGet(url); JSONObject jsonObject = JSON.parseObject(forObject); Map map = new HashMap<>(); if(jsonObject.getString("status").equals("1")){ JSONObject regeocode = jsonObject.getJSONObject("regeocode"); JSONObject addressComponent = regeocode.getJSONObject("addressComponent"); String address = regeocode.getString("formatted_address"); map.put("address", address); String code = addressComponent.getString("adcode"); String province = addressComponent.getString("province"); String city = addressComponent.getString("city"); String district = addressComponent.getString("district"); map.put("province", province); map.put("provinceCode", code.substring(0, 2) + "0000"); map.put("city", city); map.put("cityCode", code.substring(0, 4) + "00"); map.put("district", district); map.put("districtCode", code); } return map; } }