142
社区成员




高德API向开发者提供HTTP接口,开发者可通过这些接口使用各类型的地理数据服务,返回结果支持JSON和XML格式。在springboot中整合高德地图API,可以轻松实现地址定位、距离计算等功能。
首先需要在高德开发平台上进行注册,并实名认证。然后在控制台中创建新的应用并为应用添加Key:
Web服务
:这里主要是引入了fastjson:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
MapUtils类中包含getLocation
、getRealAddress
、getResponse
三个方法。getLocation
方法通过 HTTP 协议访问远程服务的地址编码接口,并从返回的json中将需要的location(经纬度)截取出来,从而实现将地址与经纬度信息之间的转化:
public String getLocation(String address) {
String url,query,result;
JSONObject temp,jsonObject;
try {
url = "http://restapi.amap.com/v3/place/around?key=" + Map_KEY
+"&location="+LONTITUDE+","+LATITUDE+"&radius=50000&keywords=" + address;
query = getResponse(url);
temp = JSONObject.parseObject(query);
String[] pois = temp.get("pois").toString().split("},\\{");
jsonObject = JSONObject.parseObject(pois[0].substring(1) + "}");
result = jsonObject.get("location").toString();
}
catch(Exception e) {
url = "http://restapi.amap.com/v3/geocode/geo?key=" + Map_KEY
+ "&address=" + address;
query = getResponse(url);
temp = JSONObject.parseObject(query);
jsonObject = JSONObject.parseObject(temp.get("geocodes")
.toString().substring(1, temp.get("geocodes").toString().length() - 1));
result = jsonObject.get("location").toString();
}
return result;
}
同理,getRealAddress
方法向API发送请求,并从返回的json中将查询到的地址截取出来,实现用户模糊搜索的地址向具体地址的转化:
public String getRealAddress(String address) {
String url,query,result;
JSONObject temp,jsonObject;
try {
url = "http://restapi.amap.com/v3/place/around?key=" + Map_KEY
+ "&location="+LONTITUDE+","+LATITUDE+"&radius=50000&keywords=" + address;
query = getResponse(url);
temp = JSONObject.parseObject(query);
String[] pois = temp.get("pois").toString().split("},\\{");
jsonObject = JSONObject.parseObject(pois[0].substring(1) + "}");
if(!jsonObject.get("address").toString().equals("[]")){
result = jsonObject.get("address").toString()+jsonObject.get("name").toString();
}
else{
result = jsonObject.get("adname").toString()+jsonObject.get("name").toString();
}
}
catch(Exception e) {
url = "http://restapi.amap.com/v3/geocode/geo?key=" + Map_KEY + "&address=" + address;
query= getResponse(url);
temp = JSONObject.parseObject(query);
jsonObject = JSONObject.parseObject(temp.get("geocodes").toString()
.substring(1, temp.get("geocodes").toString().length() - 1));
result = jsonObject.get("formatted_address").toString();
}
return result;
}
getResponse
方法通过URLConnection发送http网络请求,并以输入流的形式获取返回的json格式结果:
private static String getResponse(String serverUrl) {
StringBuffer result = new StringBuffer();
try {
URL url = new URL(serverUrl);
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
编写好工具类后,通过工具类的调用即可处理前端传来的数据并返回处理结果:
public CommonResult getLocation(String address) {
try {
String result = MapUtil.getLocation(address);
String location[]=result.split(",");
Location loc=new Location();
loc.setLongtitude(Double.parseDouble(location[0].substring(0)));
loc.setLatitude(Double.parseDouble(location[1].substring(0)));
String newAddress = MapUtil.getRealAddress(address);
loc.setAddress(newAddress);
return CommonResult.success(loc,"success");
} catch (Exception e) {
e.printStackTrace();
return CommonResult.failed("获取经纬度失败");
}
}
因为我们团队项目是关于福大校园App,因此我们的想法是将用户通过地图搜索功能能够优先返回福大周边地区,而如果直接使用高德地图api提供的地址编码会使得搜索不够精确。而如果使用高德地图的周边服务搜索又会导致搜索范围太小,很多地点无法检索。
首先通过请求以下url对福大周边地区进行搜索:
https://restapi.amap.com/v3/place/around?parameters
使用location
和radius
作为请求参数:
参数名 | 含义 | 规则说明 | 是否必须 | 缺省值 |
---|---|---|---|---|
location | 中心点坐标 | 规则: 经度和纬度用","分割,经度在前,纬度在后,经纬度小数点后不得超过6位 | 必填 | 无 |
radius | 查询半径 | 取值范围:0-50000。规则:大于50000按默认值,单位:米 | 可选 | 3000 |
若请求结果为空,则继续请求地址编码url来扩大搜索范围:
https://restapi.amap.com/v3/geocode/geo?parameters
使用请求参数city
可以对查询范围进行限制:
参数名 | 含义 | 规则说明 | 是否必须 | 缺省值 |
---|---|---|---|---|
city | 指定查询的城市 | 指定城市,当指定城市查询内容为空时,会进行全国范围内的地址转换检索。 | 可选 | 无,会进行全国范围内搜索 |