目录
一、注册账号
二、搜索小demo
1.首先要引入依赖
2. 然后查看打印结果即可
三、搜索接口代码
1.引入依赖
2.yml配置
2.Controller
3.静态工具类
四、运行测试
一、注册账号
高德开放平台 | 高德地图API
- 注册高德开发者;
- 去控制台创建应用;
- 获取Key;
具体教程可以看官方文档 创建应用和 Key-Web服务 API | 高德地图API,api文档也在链接里
二、搜索小demo
我这里用到的是关键字搜索,先写个小demo测试一下
1.首先要引入依赖
引入这个依赖主要是因为我们是通过
HttpClient
类来发送HTTP请求并获取响应的
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>
2. 然后查看打印结果即可
package cn.homed.aio.device.web;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class gaode {
public static void main(String[] args) throws UnsupportedEncodingException {
HttpClient client = HttpClients.createDefault();
String apiUrl = "https://restapi.amap.com/v5/place/text";
String apiKey = "这里填你申请的key";
String searchTest = "天安门广场";
HttpGet request = new HttpGet(apiUrl + "?key=" + apiKey + "&keywords=" + URLEncoder.encode(searchTest,"UTF-8"));
try {
HttpResponse response = client.execute(request);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、搜索接口代码
1.引入依赖
使用Fastjson的
JSONObject
类来解析HTTP响应的JSON格式数据。这样做的好处是可以方便地将HTTP响应中的JSON数据转换为Java对象
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
2.yml配置
2.Controller
这里的入参keywords是关键字,region是搜索区划
package cn.homed.common.webchat;
import cn.homed.common.constant.CommonKey;
import cn.homed.common.entity.MsgBean;
import cn.homed.common.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author hulongtao
*/
@Slf4j
@RestController
@Tag(name ="高德搜索", description = "高德搜索接口")
@RequestMapping("/api/amap/getAddressList")
public class AutonaviSearchController {
@Value("${AMAP.apiKey}")
private String apiKey;
@Value("${AMAP.apiUrl}")
private String apiUrl;
@Operation(summary = "地址搜索", description = "通用搜索")
@PostMapping(value = "")
public MsgBean getAddressList(
@RequestParam(value = "keywords") String keywords , @RequestParam(value = "region",required = false) String region
) {
if (StringUtil.isNotNullOrEmpty(keywords)) {
try {
JSONObject addressListByKeywords = AMAPSearchUtils.getAddressListByKeywords(keywords, region,apiKey,apiUrl);
if (CommonKey.CONSTANT_1.equals(addressListByKeywords.getString("status"))) {
return MsgBean.success(addressListByKeywords.getJSONArray("pois"),"搜索成功");
}
return MsgBean.failMsg("搜索异常");
}catch (Exception e){
log.error("搜索地址失败:{}",e.getMessage());
return MsgBean.fail(CommonKey.FAIL);
}
}
return MsgBean.success();
}
}
3.静态工具类
这里的city_limit返回限制
package cn.homed.common.webchat;
import cn.homed.common.constant.CommonKey;
import cn.homed.common.entity.MsgBean;
import cn.homed.common.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author hulongtao
*/
@Slf4j
@RestController
@Tag(name ="高德搜索", description = "高德搜索接口")
@RequestMapping("/api/amap/getAddressList")
public class AutonaviSearchController {
@Value("${AMAP.apiKey}")
private String apiKey;
@Value("${AMAP.apiUrl}")
private String apiUrl;
@Operation(summary = "地址搜索", description = "通用搜索")
@PostMapping(value = "")
public MsgBean getAddressList(
@RequestParam(value = "keywords") String keywords , @RequestParam(value = "region",required = false) String region
) {
if (StringUtil.isNotNullOrEmpty(keywords)) {
try {
JSONObject addressListByKeywords = AMAPSearchUtils.getAddressListByKeywords(keywords, region,apiKey,apiUrl);
if (CommonKey.CONSTANT_1.equals(addressListByKeywords.getString("status"))) {
return MsgBean.success(addressListByKeywords.getJSONArray("pois"),"搜索成功");
}
return MsgBean.failMsg("搜索异常");
}catch (Exception e){
log.error("搜索地址失败:{}",e.getMessage());
return MsgBean.fail(CommonKey.FAIL);
}
}
return MsgBean.success();
}
}
四、运行测试
可以看到,数据也是正常的