qa_kangzai/src/main/java/com/zcloud/common/utils/BaiduMapUtil.java

39 lines
1.4 KiB
Java
Raw Normal View History

2025-06-10 18:08:01 +08:00
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");
}
}