依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- sql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- json格式装换依赖 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
这里使用的是百度地图的全球逆地理编码来解析经纬度,其功能介绍我这里就不在赘述。包括百度的ak密钥如何申请,百度一下一大堆,很简单的。
逆地理编码介绍api
实体类
我们需要向百度发送一个请求,
http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194 //GET请求
通过这个请求他会返回一个json格式的信息,这个信息里面就包括详细地址信息。
现在我们需要创建这个json类来接受对应的信息
public class pojo {
private int status;
private result result;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public com.example.test.entiy.result getResult() {
return result;
}
public void setResult(com.example.test.entiy.result result) {
this.result = result;
}
@Override
public String toString() {
return "pojo{" +
"status=" + status +
", result=" + result +
'}';
}
}
public class result {
private String formatted_address;
private location location;
private addressComponent addressComponent;
private String[] pois;
private String[] roads;
private String[] poiRegions;
private String sematic_description;
private int cityCode;
public String getFormatted_address() {
return formatted_address;
}
public void setFormatted_address(String formatted_address) {
this.formatted_address = formatted_address;
}
public com.example.test.entiy.location getLocation() {
return location;
}
public void setLocation(com.example.test.entiy.location location) {
this.location = location;
}
public com.example.test.entiy.addressComponent getAddressComponent() {
return addressComponent;
}
public void setAddressComponent(com.example.test.entiy.addressComponent addressComponent) {
this.addressComponent = addressComponent;
}
public String[] getPois() {
return pois;
}
public void setPois(String[] pois) {
this.pois = pois;
}
public String[] getPoiRegions() {
return poiRegions;
}
public void setPoiRegions(String[] poiRegions) {
this.poiRegions = poiRegions;
}
public String getSematic_description() {
return sematic_description;
}
public void setSematic_description(String sematic_description) {
this.sematic_description = sematic_description;
}
public int getCityCode() {
return cityCode;
}
public void setCityCode(int cityCode) {
this.cityCode = cityCode;
}
public String[] getRoads() {
return roads;
}
public void setRoads(String[] roads) {
this.roads = roads;
}
@Override
public String toString() {
return "result{" +
"formatted_address='" + formatted_address + '\'' +
'}';
}
}
public class location {
private double lat;
private double lng;
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
}
发送请求的函数
public static Object sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
Controller
@GetMapping("/sw")
@ResponseBody
public Integer sw(){
List<shop>shopList = shopMapper.selectShop();//经纬度对应的表
for(shop shop : shopList){
String lat = shop.getLat();
String lng = shop.getLng();
String ak = "你的ak密钥";
Object s=token.sendGet("http://api.map.baidu.com/reverse_geocoding/v3/", "ak="+ak+"&output=json&coordtype=bd09ll&location="+lat+","+lng);
JSONObject jsonObject = JSONObject.fromObject(s);//将数据转换成格式
pojo po = (pojo)JSONObject.toBean(jsonObject,pojo.class);//将格式装换成实体对象
shop.setPlace(po.getResult().getFormatted_address());
shopMapper.update(shop);
System.out.println("输出:"+po);
}
return shopList.size();
}