package com.zcloud.common.utils; import com.alibaba.fastjson.JSONObject; /** * 百度地图工具类 */ public class BaiduMapUtil { // 需替换为实际的百度地图AK(从配置中心获取更佳) private static final String BAIDU_AK = "OElqFYoKiAH8KFtph8ftLKF5NlNrbCUr"; /** * 根据经纬度获取详细地址 * @param lng 经度 * @param lat 纬度 * @param coordtype 坐标系类型(wgs84ll/gcj02等) * @return 格式化地址 */ public static String getAddressByLocation(double lng, double lat, String coordtype) throws Exception { String url = String.format( "http://api.map.baidu.com/reverse_geocoding/v3/?ak=%s&output=json&coordtype=%s&location=%s,%s", BAIDU_AK, coordtype, lat, lng); // 注意百度坐标系是纬度在前 String response = HttpRequestUtil.doGet(url); JSONObject result = JSONObject.parseObject(response); if (result.getInteger("status") == 0) { return result.getJSONObject("result") .getString("formatted_address"); } throw new RuntimeException("地址解析失败:" + result.getString("message")); } // 方法重载(默认使用wgs84坐标系) public static String getAddressByLocation(double lng, double lat) throws Exception { return getAddressByLocation(lng, lat, "wgs84ll"); } }