简体   繁体   English

如何使用Google Maps API返回AND的旅行时间而没有交通?

[英]How can I use google maps API to return a journey time with AND without traffic?

I'd like a way of being able to calculate journey time WITH and without traffic, so I can work out the difference so I know how much the traffic is delaying you by. 我想要一种能够计算有无交通情况下的旅行时间的方法,因此我可以计算出差异,从而知道交通有多大延迟了您。

Also, is there a way of doing it based on a selected date and time? 另外,有没有一种方法可以根据选定的日期和时间进行呢? As I know google has this feature, just not sure if their API has made it available. 据我了解,谷歌具有此功能,只是不确定他们的API是否已将其启用。 Is it possible without loading up a map for the end visitor? 是否可以不为最终访问者加载地图?

Thanks! 谢谢!

Unfortunately Google does not support traffic data. 不幸的是Google不支持交通数据。

You can populate a map with traffic data in terms of coloured roads, which show how good/bad the traffic is ( http://code.google.com/apis/maps/documentation/javascript/examples/layer-traffic.html ). 您可以使用彩色道路来填充交通数据,以显示路况的好坏( http://code.google.com/apis/maps/documentation/javascript/examples/layer-traffic.html ) 。 However this is not supported on many roads or in many countries. 但是,许多道路或许多国家不支持此功能。

Unfortunately there is no support in the API to incorporate traffic into directions/distances. 不幸的是,API中不支持将流量合并到方向/距离中。

Google has many links with live traffic monitoring agencies who send them data. Google与实时流量监控机构有很多链接,这些实时监控机构向他们发送数据。 TomTom also collects this data and creates information like this: http://www.tomtom.com/livetraffic/ TomTom还会收集此数据并创建如下信息: http : //www.tomtom.com/livetraffic/

A lot of agencies charge developers for this data, some readily make it available as an RSS feed. 许多机构都向开发人员收取此数据的费用,有些机构很容易将其作为RSS提要使用。

It'll unfortunately be hard for you to collect all this information yourself but I think if you contact TomTom (for example) they would willingly give you their raw data live for a fee. 不幸的是,您自己很难收集所有这些信息,但是我认为,如果您与TomTom联系(例如),他们会愿意将您的原始数据实时收费提供给您。

Hope that helps. 希望能有所帮助。

您可以使用http获取和获取json中的数据为例

http://maps.googleapis.com/maps/api/distancematrix/json?origins=49.52041,11.001359&destinations=52.518271,13.408374&mode=driving&language=en-US&sensor=false

I think you will be able to journey time. 我认为您将能够度过时光。 But I am having doubt with and without traffic time api provides or not. 但是我对是否有交通时间api提供与否有疑问。

You can construct a GDirections object without a map or directions div. 您可以构建没有地图或路线div的GDirections对象。 You can do a load request from A to B and then call the getDuration method to get the total travel time. 您可以执行从A到B的加载请求,然后调用getDuration方法以获取总行驶时间。

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 use two latitude, longitudes as my start and end point, 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);

Then you need to listen for the load event so you can call getDuration once the directions have been resolved: 然后,您需要侦听load事件,以便在解决方向后可以调用getDuration

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

You can find the whole example here and the JavaScript here . 您可以在此处找到整个示例,并在此处找到JavaScript You can check the Google Maps Documentation for more info about the options you can give the GDirections object (like walking distance etc...). 您可以查看Google Maps文档以获取有关可以为GDirections对象提供选项的更多信息(例如步行距离等)。 You should also listen to the error event for geocoding failures. 您还应该侦听错误事件,以了解地理编码失败的情况。

Please refer this google map api example 请参考此Google Map API示例

http://code.google.com/apis/maps/documentation/javascript/examples/distance-matrix.html http://code.google.com/apis/maps/documentation/javascript/examples/distance-matrix.html

example: 例:

var origin1 = new google.maps.LatLng(55.930385, -3.118425);
var origin2 = "Greenwich, England";
var destinationA = "Stockholm, Sweden";
var destinationB = new google.maps.LatLng(50.087692, 14.421150);

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
  {
    origins: [origin1, origin2],
    destinations: [destinationA, destinationB],
    travelMode: google.maps.TravelMode.DRIVING,
    avoidHighways: false,
    avoidTolls: false
  }, callback);

function callback(response, status) {
  // See Parsing the Results for
  // the basics of a callback function.
}

set the distance matric parameters according to your need. 根据需要设置距离矩阵参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以(以及如何)在没有API的情况下使用样式化的Google地图 - Can I (and how to) use styled google maps without API 使用Google Maps API计算两个位置(坐标)之间的旅行时间 - Calculate journey time between two locations (co-ordinates) using google maps api 如何在没有 api 密钥的情况下使用谷歌地图 - How to use google maps without api key 我可以在Google Maps javascript API v3中隐藏路况图层的绿色吗? - Can I hide the green color of traffic layer in Google maps javascript api v3? 用于实时交通的谷歌地图 API - Google maps API for live traffic 我可以通过 react-google-maps/api 使用 Google 地方信息吗? - Can I use Google Places with react-google-maps/api? 如何使用Google Maps JavaScript API显示路线的路况? - How can you display traffic for a route using the Google Maps JavaScript API? 如何将位智事件标记添加到带有流量的Google Maps javascript API生成的地图中? - How do I add waze incident markers to a google maps javascript API-generated map with traffic? 如何在Google Maps API中的路线折线上显示交通图层 - How to show Traffic layer on top of Directions polyline in google maps API 我可以在后端AJAX中使用Google Maps API吗? - Can I use Google Maps API in backend AJAX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM