使用Google Roads API抓取道路信息(java实现)

Google Roads API提供了强大的道路获取接口,用户仅仅需调用对应的API就能够获取对应区域的道路坐标以及道路的限速信息。

详细的调用方法例如以下:

https://roads.googleapis.com/v1/snapToRoads?

path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836


       
& interpolate = true
       
& key = API_KEY 当中以“|”切割开的是经纬度坐标。假设将interpolate设置为true,则会返回平滑的路线信息。

key的值须要替换为用户自己申请的key值。

普通开发人员每天能够运行上述查询2500次。Java调用Roads API的代码例如以下:

StringBuffer document = new StringBuffer();
        try {
            String str_url = "https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836&interpolate=true&key=YourKeyHere";
            URL url = new URL(str_url);//远程url
            URLConnection conn = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            int line_number = 0;
            while ((line = reader.readLine()) != null) {
                line_number += 1;
                if (line.startsWith("        \"latitude\": ")) {
                    line = line.replaceAll("        \"latitude\": ", "");
                    document.append(line + " ");
                    System.out.print(line);
                }
                if (line.startsWith("        \"longitude\": ")) {
                    line = line.replaceAll("        \"longitude\": ", "");
                    document.append(line + " ");
                    System.out.println(line);
                }
            }
            if (line_number < 20) {
                System.out.println("error");
            }
            reader.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

查询后的结果例如以下图所看到的:



转载于:https://www.cnblogs.com/lcchuguo/p/5079627.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值