腾讯地图开放api
地址解析
https://lbs.qq.com/service/webService/webServiceGuide/webServiceGeocoder
public class MapHttpClientUtils {
//地址解析
private static final String GEOCODER_BASE_URL = "https://apis.map.qq.com/ws/geocoder/v1/";
//腾讯地图key
private static final String TENCENT_MAP_KEY = "这里是你的key";
//申请key https://lbs.qq.com/mobile/androidMapSDK/developerGuide/getKey
/**
* 获取经纬度
* @param address
* @return
* @throws IOException
*/
public static BigDecimal[] location(String address) throws IOException {
JSONObject jsonObject = geocoderRequest(address);
if (ObjectUtil.isNullOrEmpty(jsonObject)) {
return null;
}
JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
//经度
BigDecimal lng = location.getBigDecimal("lng");
//纬度
BigDecimal lat = location.getBigDecimal("lat");
return new BigDecimal[]{lng,lat};
}
/**
* 地址解析 https://lbs.qq.com/service/webService/webServiceGuide/webServiceGeocoder
* @param address
* @return
*/
private static JSONObject geocoderRequest(String address) {
try {
String encodedAddress = URLEncoder.encode(address, "UTF-8");
String requestUrl = GEOCODER_BASE_URL + "?keyword=" + encodedAddress + "&key=" + TENCENT_MAP_KEY;
JSONObject jsonObject = new JSONObject(sendGetRequest(requestUrl));
int status = jsonObject.getInt("status");
String message = jsonObject.getString("message");
if (status == 0) {
return jsonObject;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* GET 请求
* @param requestUrl
* @return
* @throws IOException
*/
private static String sendGetRequest(String requestUrl) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(requestUrl);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
httpClient.close();
return responseBody;
}
}
地址解析结果: