使用百度地图SDK计算距离

说明:通过百度地图提供的SDK,可以计算出两个地点之间的距离,另外还有行驶路线等等。本文介绍如果使用百度地图SDK,并用java代码实现。

申请

首先需要登录百度地图的官网,申请开发者认证,个人认证一般都很快

获取AK

第一步:选择地理编码

在这里插入图片描述

第二步:获取AK

在这里插入图片描述

在这里插入图片描述

第三步:设置应用名

在这里插入图片描述

在这里插入图片描述

第四步:复制AK

应用创建完成后,就可以达到一个AK,后面会用到;

在这里插入图片描述

接口一:地理编码

计算距离前,先要获取点地点的经纬度,找到刚刚地理编码里面的接口文档,查看这个接口文档的描述;

在这里插入图片描述

不难理解,只要把这个链接中的这两个地方替换成我们需要计算的地点信息,加上AK就可以了。

在这里插入图片描述

用postman测试一下,果然可以得到经纬度坐标;

在这里插入图片描述

接口二:计算距离

找到驾车路线规划的接口文档,查看接口文档说明,也不难理解,这个接口也是访问一个链接,把链接中的信息,换成需要计算距离的两个地点的经纬度,加上自己的AK;

在这里插入图片描述

按照接口文档中提供的地址,用postman测试一下,可以看到返回的结果信息很多,有距离和驾驶时长,待会儿我们就用代码取出距离这个信息;

在这里插入图片描述

代码实现

以下是我编写的代码,亲测有效,可供参考,使用需要导入hutool工具类的依赖

	<dependency>
	    <groupId>cn.hutool</groupId>
	    <artifactId>hutool-all</artifactId>
	    <version>5.8.6</version>
	</dependency>
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.List;

/**
 * 百度地图工具类
 */
public class BaiduUtils {
    
    

    /**
     * 计算两点之间的距离
     *
     * @param address1 地点1
     * @param address2 地点2
     * @param AK
     * @return
     */
    public static Long computerDistance(String address1, String address2, String AK) {
    
    
        
        // 获取两点坐标
        Double[] location1 = getPosition(address1, AK);
        Double[] location2 = getPosition(address2, AK);

        // 计算记录
        String bdResponse = HttpUtil.get("https://api.map.baidu.com/directionlite/v1/driving?origin="
                + location1[0] + "," + location1[1] + "&destination=" + location2[0] + "," + location2[1] + "&ak=" + AK + "&riding_type=1");

        // 空值判断
        if (StrUtil.isBlank(bdResponse)) {
    
    
            System.out.println("返回信息为空");
        }

        // 解析bdResponse
        JSONObject result = null;
        try {
    
    
            result = JSONUtil.parseObj(bdResponse);
        } catch (Exception e) {
    
    
            e.getMessage();
        }

        // 获取距离并返回
        Long distance = Convert.toLong(JSONUtil.parseObj(result.getJSONObject("result").get("routes", List.class).get(0)).get("distance"));

        return distance;
    }

    /**
     * 获取地址的坐标
     *
     * @param address
     * @param AK
     * @return
     */
    private static Double[] getPosition(String address, String AK) {
    
    

        // 封装参数
        HashMap<String, Object> mm = new HashMap<>();
        mm.put("address", address);
        mm.put("ak", AK);
        mm.put("output", "json");

        // 发送请求
        String bdResponse = HttpUtil.get("https://api.map.baidu.com/geocoding/v3", mm);

        // 空值判断
        if (StrUtil.isBlank(bdResponse)) {
    
    
            System.out.println("返回信息为空");
        }

        JSONObject shopJsonObject = null;

        // 解析bdResponse
        try {
    
    
            shopJsonObject = JSONUtil.parseObj(bdResponse);
        }catch (Exception e) {
    
    
            e.getMessage();
        }

        // 获取坐标并返回
        Double lat = shopJsonObject.getJSONObject("result").getJSONObject("location").get("lat", Double.class);
        Double lng = shopJsonObject.getJSONObject("result").getJSONObject("location").get("lng", Double.class);

        return new Double[]{
    
    lat,lng};
    }

    public static void main(String[] args) {
    
    
        String address1 = "杭州东站";
        String address2 = "杭州西站";

        String AK = "您的AK";

        System.out.println(computerDistance(address1, address2, AK) + "米");
    }
}

计算杭州东站到杭州西站的距离;

在这里插入图片描述

需要注意,计算距离这里提供了四种交通方式,如果选择的是骑行,并且距离又选择的很远的话,程序会报空指针异常,这点在项目中使用时需要考虑到。可能距离如果太远,不管选择是哪种交通方式,都会报空指针异常,可以试一试。

在这里插入图片描述

总结

使用百度地图SDK计算距离,需要发两次请求,一次返回地点的经纬度,第二次使用地点的经纬度计算记录,使用起来非常遍历,不需要导入额外的依赖。

另外,如果你需要路线规划、驾车时长等信息,可以根据返回结果解析。

猜你喜欢

转载自blog.csdn.net/qq_42108331/article/details/131653935
今日推荐