调用百度的接口
/**
* 根据经纬度查询详细地址
* https://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
*
* @param longitude 经度
* @param latitude 纬度
*/
public static BaiDuMapResultVO getBaiDuAddress(Double longitude, Double latitude) {
String url = "https://api.map.baidu.com/reverse_geocoding/v3";
Map<String, Object> map = new HashMap<>();
map.put("ak", "yvX3icmfIG62DZHcrirvq6kpbtxFKFTE");
map.put("output", "json");
map.put("coordtype", "wgs84ll");
map.put("location", latitude + "," + longitude);
String data = HttpUtil.get(url, map);
BaiDuMapVO baiDuMapVO = JSONUtil.toBean(data, BaiDuMapVO.class);
if (baiDuMapVO == null || baiDuMapVO.getStatus() != 0) {
System.out.println("异常");
} else {
return baiDuMapVO.getResult();
}
return null;
}
封装返回给前端的数据
package com.community.web.controller.openApi;
public class BaiDuMapResultVO {
/**
*结构化地址信息
*/
private String formatted_address;
/**
* 坐标所在商圈信息,如 "人民大学,中关村,苏州街"。最多返回3个。
*/
private String business;
/**
* 当前位置结合POI的语义化结果描述。需设置extensions_poi=1才能返回。
*/
private String sematic_description;
/**
* 百度定义的城市id(正常更新与维护,但建议使用adcode)
*/
private String cityCode;
/**
* (注意,国外行政区划,字段仅代表层级)
*/
private BaiDuMapResultAddressVO addressComponent;
/**
* 经纬度坐标
*/
private Object location;
public String getFormatted_address() {
return formatted_address;
}
public void setFormatted_address(String formatted_address) {
this.formatted_address = formatted_address;
}
public String getBusiness() {
return business;
}
public void setBusiness(String business) {
this.business = business;
}
public String getSematic_description() {
return sematic_description;
}
public void setSematic_description(String sematic_description) {
this.sematic_description = sematic_description;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public BaiDuMapResultAddressVO getAddressComponent() {
return addressComponent;
}
public void setAddressComponent(BaiDuMapResultAddressVO addressComponent) {
this.addressComponent = addressComponent;
}
public Object getLocation() {
return location;
}
public void setLocation(Object location) {
this.location = location;
}
}
package com.community.web.controller.openApi;
public class BaiDuMapResultAddressVO {
/**
* 城市所在级别(仅国外有参考意义。国外行政区划与中国有差异,城市对应的层级不一定为『city』。country、province、city、district、town分别对应0-4级,若city_level=3,则district层级为该国家的city层级)
*/
private String city_level;
/**
* 国家
*/
private String country;
/**
* 省名
*/
private String province;
/**
* 城市名
*/
private String city;
/**
* 区县名
*/
private String district;
/**
* 街道名(行政区划中的街道层级
*/
private String street;
/**
* 街道门牌号
*/
private String street_number;
/**
* 行政区划代码,前往下载
*/
private String adcode;
/**
* 乡镇名,需设置extensions_town=true时才会返回
*/
private String town;
/**
* 相对当前坐标点的距离,当有门牌号的时候返回数据
*/
private String distance;
/**
* 国家英文缩写(三位)
*/
private String country_code_iso;
/**
* 国家英文缩写(两位)
*/
private String country_code_iso2;
/**
* 国家编码
*/
private String country_code;
/**
* 乡镇id
*/
private String town_code;
/**
* 相对当前坐标点的方向,当有门牌号的时候返回数据
*/
private String direction;
public String getCity_level() {
return city_level;
}
public void setCity_level(String city_level) {
this.city_level = city_level;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getStreet_number() {
return street_number;
}
public void setStreet_number(String street_number) {
this.street_number = street_number;
}
public String getAdcode() {
return adcode;
}
public void setAdcode(String adcode) {
this.adcode = adcode;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getCountry_code_iso() {
return country_code_iso;
}
public void setCountry_code_iso(String country_code_iso) {
this.country_code_iso = country_code_iso;
}
public String getCountry_code_iso2() {
return country_code_iso2;
}
public void setCountry_code_iso2(String country_code_iso2) {
this.country_code_iso2 = country_code_iso2;
}
public String getCountry_code() {
return country_code;
}
public void setCountry_code(String country_code) {
this.country_code = country_code;
}
public String getTown_code() {
return town_code;
}
public void setTown_code(String town_code) {
this.town_code = town_code;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
}
封装百度接口返回的数据
package com.community.web.controller.openApi;
public class BaiDuMapVO {
/**
* 0 正常 其他错误
*/
private Integer status;
/**
* 当status不为0时 这个字段有值
*/
private String message;
/**
* 数据
*/
private BaiDuMapResultVO result;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public BaiDuMapResultVO getResult() {
return result;
}
public void setResult(BaiDuMapResultVO result) {
this.result = result;
}
}
测试调用
public static void main(String[] args) {
BaiDuMapResultVO baiDuAddress = getBaiDuAddress(116.258815, 40.038564);
System.out.println(baiDuAddress.getFormatted_address());
}
结果
北京市海淀区永丰路