通过经纬度调用百度sdk api实现查询详细地址 Java

依赖

<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();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值