使用谷歌地图API来获取旅行时间的数据 [英] Using Google Maps API to get travel time data

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

问题描述

我遇到使用谷歌地图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​​,这是绝对有可能的。你可以构造一个GDirections对象没有地图或路线股利。您可以从做一个加载请求到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);

然后,你需要监听的负荷的事件,因此可以调用的 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";
        });

您可以找到href=\"http://www.cannonade.net/geo.php?test=geo6\"这里 rel=\"nofollow\">整个例如此处的JavaScript 。您可以查看谷歌地图文档了解更多信息有关选项,你可以给GDirections对象(如步行距离等)。你也应该听的错误的事件进行地理编码失败。

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.

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

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