利用高德api得到路线规划线路的点坐标,并在java中利用它

        最近需要GPS的点坐标,但没有真实的数据,就只能来模拟它。我需要的仅仅是点的经纬度信息,不需要什么速度等数据,所以我就提取高德api得到路线规划线路的点坐标来作为gps数据使用,模拟电动车的运动。做了一个工具类,传输起点和终点参数,就可以得到路线点坐标集合。

1.   首先需要获取到高德的web服务的key, 可以自己申请一下。

https://restapi.amap.com/v4/direction/bicycling?origin=起点&destination=终点&key=你申请的key

上述网址可以得到骑行路径的线路规划结果,以JSON数据返回,包含我们需要的点坐标数据,路径是高德推荐的最好骑行结果,如果想要得到驾车或步行的结果可以参考:高德地图Web API开发文档:路径规划-API文档-开发指南-Web服务 API | 高德地图API

2. 利用Python解析JSON数据,得到点数据,并将Python变为一个函数,以便之后java调用它。

# coding=utf-8
import json
import requests
import sys


print("你好")
def hello(a, b):
    url = f"https://restapi.amap.com/v4/direction/bicycling?origin={a}&destination={b}&key=你申请的key"

    response = requests.get(url)  # 发送GET请求
    s = ''
    if response.status_code == 200:  # 如果请求成功
        s = response.content.decode('utf-8')  # 将返回的二进制数据转换为字符串
    else:
        print('请求失败')
        return

    # 将json数据转为Python字典
    data = json.loads(s)

    # 获取polyline信息
    polyline = []
    for step in data['data']['paths'][0]['steps']:
        polyline += step['polyline'].split(';')
    s1 = ';'.join(polyline)

    return s1


a = sys.argv[1]
b = sys.argv[2]
print(hello(a, b))

3. 验证是否成功,打开cmd,输入命令 :python 你写的Python文件的绝对路径 起点 终点

 例子如下:第一行”你好”之后就是我们需要的点坐标数据,它们以;分割开来。

4. 如果你只需要得到一条路径的点坐标集合的话,只需将上面结果复制即可,但我需要的得到多条线路的点坐标集合,所以做成java工具类会好一点。

    public static String getPoints(String src,String dest) throws IOException, InterruptedException {
        String[] arg = new String[] { "python", "D:\\大学课程资料\\python\\JSONParser\\routingTool02.py", src, dest };
        Process process = Runtime.getRuntime().exec(arg);


        // 读取 Python 脚本的输出
        InputStream inputStream = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String output = reader.readLine();
//        System.out.println(output);
        String readLine;
        StringBuffer stringBuffer=new StringBuffer();
        while ((readLine= reader.readLine())!=null){
            stringBuffer.append(readLine);
        }

        //获取错误信息
        InputStream errorStream = process.getErrorStream();
        BufferedReader readerError = new BufferedReader(new InputStreamReader(errorStream));
        StringBuffer errorBuffer = new StringBuffer();
        String errorLine;
        while ((errorLine = readerError.readLine()) != null) {
            errorBuffer.append(errorLine).append("\n");
        }

        // 获取退出代码
        int exitCode = process.waitFor();

        // 打印输出和退出代码
//        System.out.println(stringBuffer);
//        System.out.println(exitCode);

        if (errorBuffer.length() > 0) {
            System.err.println(errorBuffer.toString());
        }
        //返回结果
        return stringBuffer.toString();
    }

5. 应用验证一下是否成功

  public static void main(String[] args) throws IOException, InterruptedException {

        String a="113.357273,23.15621";
        String b="113.366065,23.159905";
        String points = Utility.getPoints(a, b);
        System.out.println(points);
      
    }

结果输出如下就代表已经成功了,之后就可以模拟各种线路点集合了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shunrai1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值