需求:通过一组经纬度查询地址
高德api:入门指南-Web服务 API | 高德地图API (amap.com)
先申请高德key
接口调用:keyValue替换自己的keyValue
batch:是否批量查询,默认false不使用
https://restapi.amap.com/v3/geocode/regeo?key=keyValue&location=112.286928,31.548141&batch=true&extensions =all
代码调用:
public String getAddress(double latitude, double longitude) {
String address = "";
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = null;
String location = longitude + "," + latitude;
//调用高德地图请求拿到数据
try {
uriBuilder = new URIBuilder("https://restapi.amap.com/v3/geocode/regeo");
uriBuilder.setParameter("key", mapConfiguation.getGaodeKey())
.setParameter("location", location);
HttpGet httpGet = new HttpGet(uriBuilder.build());
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
String content = EntityUtils.toString(httpEntity, "utf8");
//解析数据
address = GaodeLocationByLongitudeAndlatitude(content.toString());
// System.out.println("位置信息=================="+address);
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return address;
}
/**
* 输入经纬度后返回位置信息
*
* @param jsonData
* @return 返回的类gaodelocation,详见类
*/
public String GaodeLocationByLongitudeAndlatitude(String jsonData) {
String formattedAddress = null;
String info = null;
String status = null;
String address = null;
try {
JSONObject jsonObject = JSONObject.fromObject(jsonData);
// 解析json
status = jsonObject.getString("status");
info = jsonObject.getString("info");
JSONObject dataObj = jsonObject.getJSONObject("regeocode");
formattedAddress = dataObj.getString("formatted_address");
if (status.equals("1")) {
address = formattedAddress;
} else {
address = info;
}
} catch (Exception e) {
e.printStackTrace();
address = e.getMessage();
}
return address;
}
由于多条数据一条条查询开始使用ls.stream()查询,销量较低
后来改为parallelStream()效率大大提高