cn.takujo.common_api.util.TencentLocationUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-api Show documentation
Show all versions of common-api Show documentation
takujoframework common-api
package cn.takujo.common_api.util;
import java.util.Map;
import cn.takujo.common_api.exception.HttpException;
import cn.takujo.common_api.exception.JsonException;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* 腾讯地图服务
*
* @author wzx
*
*/
@RequiredArgsConstructor
@Data
public final class TencentLocationUtil {
/**
* 腾讯地图开发者的key
*/
@NonNull
private String key;
private static final String URL = "https://apis.map.qq.com/ws/geocoder/v1/";
/**
* 首选地址(腾讯地图推荐的地址)
*
* @param lat
* 纬度
* @param lng
* 经度
* @return 返回首选地址,无匹配则返回空字符串
*/
@SuppressWarnings("unchecked")
public String recommendAddress(String lat, String lng) {
String location = lat + "," + lng;
try {
String str = HttpUtil.get(URL + "?location=" + location + "&key=" + key);
Map map = JsonUtil.toMap(str);
Map result = (Map) map.get("result");
Map formatted_addresses = (Map) result.get("formatted_addresses");
String address = (String) result.get("address");
String recommend = (String) formatted_addresses.get("recommend");
return address + recommend;
} catch (HttpException e) {
e.printStackTrace();
} catch (JsonException e) {
e.printStackTrace();
}
return "";
}
}