前言:
调用高德的关键字搜索接口,把获得的数据解析,然后存到自己的数据库中,可以获得你想要的经纬度、地址、邮编等信息。
本次调用的是搜索服务API是一类简单的HTTP接口,提供多种查询POI信息的能力,其中包括关键字搜索、周边搜索、多边形搜索、ID查询四种筛选机制
官网:https://lbs.amap.com/api/webservice/guide/api/search
使用说明:
第一步,申请”Web服务API”密钥(Key);
第二步,拼接HTTP请求URL,第一步申请的Key需作为必填参数一同发送;
第三步,接收HTTP请求返回的数据(JSON或XML格式),解析数据。
如无特殊声明,接口的输入参数和输出数据编码全部统一为UTF-8。
具体步骤:
1、在官网上创建引用,并获得请求接口所用到的key
2、根据接口发起请求
URL | https://restapi.amap.com/v3/place/text |
请求方式 | GET |
示例:
用的的依赖包
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
@GetMapping("/getInfos")
public ResponseEntity getInfo(@RequestParam String keyWord, @RequestParam String city){
String url = "https://restapi.amap.com/v3/place/text?keywords="+keyWord+"&city="+city+"&output=JSON&offset=2000&page=0&key=2c29679501f73bb9f587a8cf0a87ed13&citylimit=true&extensions=all";
GaodeLocation result = gaoDeMapUtil.getLocatoin(url);
return ResponseEntity.ok(result);
}
/**
* 输入地址返回识别后的信息
*
* @param address
* @return 返回的类gaodelocation,详见类
*/
public GaodeLocation getLocatoin(String address) {
GaodeLocation location = null;
if (address != null) {
try {
String result = "";
location = new GaodeLocation();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(address).build();
try {
Response response = client.newCall(request).execute();
result = response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
// String result = OKHttpUtil.httpGet(address);
JSONObject jsonObject = JSONObject.parseObject(result);
// 解析json
location.setStatus(jsonObject.getString("status"));
location.setInfo(jsonObject.getString("info"));
location.setCount(jsonObject.getString("count"));
List<Geocodes> geocodes = new ArrayList<>();
JSONArray jsonArray = jsonObject.getJSONArray("pois");
// 遍历解析出来的结果
if ((jsonArray != null) && (jsonArray.size() > 0)) {
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jo = (JSONObject) jsonArray.get(i);
Geocodes go = new Geocodes();
go.setFormatted_address(jo.getString("address"));
go.setProvince(jo.getString("pname"));
go.setCitycode(jo.getString("citycode"));
go.setCity(jo.getString("cityname"));
go.setDistrict(jo.getString("adname"));
// 地址所在的乡镇
JSONArray ts = jo.getJSONArray("township");
if (ts != null && ts.size() > 0) {
go.setTownship(ts.getString(0));
}
// 地址编号
go.setAdcode(jo.getString("adcode"));
// 街道
JSONArray jd = jo.getJSONArray("street");
if (jd != null && jd.size() > 0) {
go.setStreet(jd.getString(0));
}
// 号码
JSONArray hm = jo.getJSONArray("number");
if (hm != null && hm.size() > 0) {
go.setStreet(hm.getString(0));
}
go.setLocation(jo.getString("location"));
go.setLevel(jo.getString("level"));
geocodes.add(go);
}
}
locationRepository.saveAll(geocodes);
location.setGeocodes(geocodes);
} catch (Exception e) {
e.printStackTrace();
}
}
return location;
}
对应的实体类
@Data
public class GaodeLocation {
private String status;// 结果状态0,表示失败,1:表示成功
private String count;// 返回结果的数目
private String info;// 返回状态说明
private List<Geocodes> geocodes;// 识别的结果列表
}
@Data
@Table(name = "address")
@Entity
public class Geocodes {
@Id
@GenericGenerator(name = "user-info-uuid", strategy = "uuid")
@GeneratedValue(generator = "user-info-uuid")
@Column(name = "id")
private String id;
// 结构化地址信息
@Column(name = "formatted_address")
private String formatted_address;
// 所在省
@Column(name = "province")
private String province;
// 城市
@Column(name = "city")
private String city;
// 城市编码
@Column(name = "citycode")
private String citycode;
// 地址所在的区
@Column(name = "district")
private String district;
// 地址所在的乡镇
@Column(name = "township")
private String township;
// 街道
@Column(name = "street")
private String street;
// 门牌
@Column(name = "number")
private String number;
// 区域编码
@Column(name = "adcode")
private String adcode;
// 坐标点
@Column(name = "location")
private String location;
// 匹配级别
@Column(name = "level")
private String level;
}
3、注意事项:
高德的接口返回的数据可以是json格式还可以是xml格式,本次解析的是json格式。
注意:在此接口之中,您可以通过city&citylimit参数指定希望搜索的城市或区县。而city参数能够接收citycode和adcode,citycode仅能精确到城市,而adcode却能够精确到区县。
例如:北京,citycode:010,adcode:110000
北京-海淀区,citycode:010,adcode:110108
故使用citycode仅能在北京范围内搜索,而adcode能够指定在海淀区搜索。
综上所述,为了您查询的精确,我们强烈建议您使用adcode。
另外,无论您指定多少个type,每次请求最多返回1000个POI信息,若场景需要获取更可能多的POI;建议您不要在type之中指定过多的类别,而是分多次请求从而得到更加准确的结果。
下载code编码地址:https://lbs.amap.com/api/webservice/download
如果有什么不懂,可以在评论区随时联系我