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

39 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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");
}
}