百度地图开发api
获取百度地图ak:https://zhuanlan.zhihu.com/p/574521243
引入依赖:
<!-- 引入HttpClient依赖 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.79</version> </dependency>
在yml中配置百度地图AK和地理编码
#百度地图 AK baidu: ak: MKUZ0Oxzks86zaPstt url: http://api.map.baidu.com/geocoding/v3/?output=json&location=showLocation rurl: http://api.map.baidu.com/reverse_geocoding/v3/?output=json&coordtype=BD09&pois=1
创建返回地理位置信息实体类
实体类信息
package org.jeecg.modules.baidu;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author MYM_ZUOYAN
* @version 2.0
* @date 2021/4/9 15:03
* 位置信息返回实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ReturnLocationBean implements Serializable {
private static final Long serializableValue = 1L;
/**
* 地理位置
*/
private String formattedAddress;
/**
* 经度
*/
private Double lng;
/**
* 纬度
*/
private Double lat;
/**
* 品级
*/
private String level;
}
控制层信息
import org.springframework.web.bind.annotation.RestController; @Slf4j @Api(tags = "百度信息") @RestController @RequestMapping("/statistics/baidu") public class BaiduController { /** * 百度 AK */ @Value("${baidu.ak}") private String AK; /** * 地理编码 URL */ @Value("${baidu.url}") private String ADDRESS_TO_LONGITUDEA_URL; /** * 逆地理编码 URL */ @Value("${baidu.rurl}") private String LONGITUDE_TO_ADDRESS_URL; /** * @param address * 根据地理位置获取经纬度信息 * @return */ @AutoLog(value = "百度信息-根据地理位置获取经纬度信息") @ApiOperation(value = "百度信息-根据地理位置获取经纬度信息", notes = "百度信息-根据地理位置获取经纬度信息") @PostMapping("/addressToLngLat") public Result<?> addressToLngLat(String address) { if (StringUtils.isBlank(address)) { return Result.error("地理位置不能为空!"); } else { String url = ADDRESS_TO_LONGITUDEA_URL + "&ak=" + AK + "&address=" + address; // 创建默认http连接 HttpClient client = HttpClients.createDefault(); //创建一个post请求 HttpPost post = new HttpPost(url); try { // 用http连接去执行get请求并且获得http响应 HttpResponse response = client.execute(post); // 从response中取到响实体 HttpEntity entity = response.getEntity(); // 把响应实体转成文本 String html = EntityUtils.toString(entity); //转Json实体 JSONObject jsonObject = JSON.parseObject(html); //地理信息实体封装 ReturnLocationBean locationBean = new ReturnLocationBean(); locationBean.setLng(jsonObject.getJSONObject("result").getJSONObject("location").getDouble("lng")); locationBean.setLat(jsonObject.getJSONObject("result").getJSONObject("location").getDouble("lat")); locationBean.setLevel(jsonObject.getJSONObject("result").getString("level")); locationBean.setFormattedAddress(address); return Result.OK(locationBean); } catch (Exception e) { return Result.error("查询失败!"); } } } /** * 逆地理编码,根据经纬度获取地理位置 * @param lat * @param lng * @return */ @AutoLog(value = "百度信息-根据经纬度获取地理位置") @ApiOperation(value = "百度信息-根据经纬度获取地理位置", notes = "百度信息-根据经纬度获取地理位置") @PostMapping("/longitudeToAddress") public Result<?> longitudeToAddress(float lat, float lng) { String url = LONGITUDE_TO_ADDRESS_URL + "&ak=" + AK + "&location=" + lat + "," + lng; // 创建默认http连接 HttpClient client = HttpClients.createDefault(); // 创建一个post请求 HttpPost post = new HttpPost(url); try { // 用http连接去执行get请求并且获得http响应 HttpResponse response = client.execute(post); // 从response中取到响实体 HttpEntity entity = response.getEntity(); // 把响应实体转成文本 String html = EntityUtils.toString(entity); //转Json实体 JSONObject jsonObject = JSON.parseObject(html); //地理信息实体封装 ReturnLocationBean locationBean = new ReturnLocationBean(); locationBean.setLng((double) lng); locationBean.setLat((double) lat); locationBean.setFormattedAddress(jsonObject.getJSONObject("result").getString("formatted_address")); return Result.OK(locationBean); } catch (Exception e) { return Result.error("查询失败!"); } } }