使用 Google Maps API 获取行程时间数据 [英] Using Google Maps API to get travel time data

查看:85
本文介绍了使用 Google Maps API 获取行程时间数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Google Maps API 遇到的所有示例似乎都显示了某种地图.当您要求从 A 到 B 的道路描述时,我想将他们提供的有关汽车估计旅行时间的数据合并到一个站点中.而且只有那个数据.是否可以不为最终访问者加载地图?

All the examples I've come across using Google Maps API seem to show a map of some kind. I would like to incorporate the data about the estimated travel time by car they give you when you ask for a road description from A to B into a site. And only that data. Is it possible without loading up a map for the end visitor?

推荐答案

是的,这绝对可以使用 API 实现.您可以在没有地图或方向 div 的情况下构建 GDirections 对象.您可以执行从 A 到 B 的加载请求,然后调用 getDuration 获取总行程时间的方法.

Yep, this is definitely possible using the API. You can construct a GDirections object without a map or directions div. You can do a load request from A to B and then call the getDuration method to get the total travel time.

首先你需要创建一个方向对象:

First you need to create a directions object:

// no need to pass map or results div since
// we are only interested in travel time.
var directions = new GDirections (); 

然后执行加载请求以解析方向(我使用了两个纬度和经度作为起点和终点,但您也可以在此处使用地址):

Then do your load request to resolve the directions (I have used two latitudes and longitudes as my start and end points, but you can use addresses here as well):

var wp = new Array ();
wp[0] = new GLatLng(32.742149,119.337218);
wp[1] = new GLatLng(32.735347,119.328485);
directions.loadFromWaypoints(wp);

然后您需要监听 load 事件,以便在路线确定后调用 getDuration:

Then you need to listen for the load event so you can call getDuration once the directions have been resolved:

GEvent.addListener(directions, "load", function() {
    $('log').innerHTML = directions.getDuration ().seconds + " seconds";
        });

您可以在此处找到整个示例JavaScript 在这里.您可以查看 Google 地图文档,了解有关选项的更多信息您可以提供 GDirections 对象(如步行距离等...).对于地理编码失败,您还应该监听 error 事件.

You can find the whole example here and the JavaScript here. You can check the Google Maps Documentation for more info about the options you can give the GDirections object (like walking distance etc...). You should also listen to the error event for geocoding failures.

这篇关于使用 Google Maps API 获取行程时间数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆