Fel*_*sen 33 api google-maps travel-time
我使用谷歌地图api遇到的所有例子似乎都显示了某种地图.当您要求从A到B进入某个站点的道路描述时,我想通过他们为您提供的汽车估计行程时间数据.而且只有那些数据.是否可以在不为最终访问者加载地图的情况下?
小智 23
在上面的答案是违反的服务的谷歌API的条款,因此,不正确.
Google Maps API仅允许您计算旅行时间,如果它是针对向用户显示的Google地图引用的.
如果您未向服务的最终用户显示Google Map,则无法使用API.
更新:
这里的任何答案,都没有显示在谷歌地图上映射的最终结果,违反了上述服务条款,最终是不正确的.
Red*_*ing 20
是的,这绝对可以使用API.您可以构建一个没有地图或方向div的GDirections对象.您可以从A到B执行加载请求,然后调用getDuration方法以获取总行程时间.
首先,您需要创建路线对象:
// no need to pass map or results div since
// we are only interested in travel time.
var directions = new GDirections ();
Run Code Online (Sandbox Code Playgroud)
然后执行加载请求以解决方向(我使用了两个纬度和经度作为我的起点和终点,但你也可以在这里使用地址):
var wp = new Array ();
wp[0] = new GLatLng(32.742149,119.337218);
wp[1] = new GLatLng(32.735347,119.328485);
directions.loadFromWaypoints(wp);
Run Code Online (Sandbox Code Playgroud)
然后你需要监听load事件,这样你就可以在解决了方向后调用getDuration:
GEvent.addListener(directions, "load", function() {
$('log').innerHTML = directions.getDuration ().seconds + " seconds";
});
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到整个例子和JavaScript.您可以查看Google地图文档,了解有关您可以为GDirections对象提供的选项的更多信息(例如步行距离等).您还应该收听错误事件以查找地理编码失败.
Igo*_*rra 20
记录:此时(2015年)不推荐使用 GDirections,GLatLng,GEvent等.
您可以使用google.maps.DirectionsService类(Google Maps JavaScript API V3)
在以下代码段中,位置和目标是包含纬度和经度坐标的对象.
1)google.maps.DirectionsService类允许LatLng和字符串值提供其原始和目标属性.
2)LatLng是地理坐标中的一个点:纬度和经度.
var origin = new google.maps.LatLng( location.latitude, location.longitude ); // using google.maps.LatLng class
var destination = target.latitude + ', ' + target.longitude; // using string
var directionsService = new google.maps.DirectionsService();
var request = {
origin: origin, // LatLng|string
destination: destination, // LatLng|string
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route( request, function( response, status ) {
if ( status === 'OK' ) {
var point = response.routes[ 0 ].legs[ 0 ];
$( '#travel_data' ).html( 'Estimated travel time: ' + point.duration.text + ' (' + point.distance.text + ')' );
}
} );
Run Code Online (Sandbox Code Playgroud)
来自google.maps.DirectionsRenderer
使用以下距离:
directionsDisplay.directions.routes[0].legs[0].distance.text
Run Code Online (Sandbox Code Playgroud)
持续时间通过使用:
directionsDisplay.directions.routes[0].legs[0].duration.text
Run Code Online (Sandbox Code Playgroud)
仅当以下所有条件都为真时才返回流量持续时间:
该请求包括一个离开时间参数。该请求包含有效的 API 密钥,或有效的 Google Maps APIs Premium Plan 客户端 ID 和签名。所请求路线的交通状况是可用的。模式参数设置为驱动。
至少从 2019 年 5 月起,您现在可以在有或没有地图的情况下显示行驶时间。
您可以在 Google 地图上或没有地图上显示距离矩阵 API 结果。如果您想在地图上显示距离矩阵 API 结果,那么这些结果必须显示在 Google 地图上。禁止在非 Google 地图的地图上使用距离矩阵 API 数据。
请注意,Mapquest 不会根据实际交通情况显示行驶时间。
| 归档时间: |
|
| 查看次数: |
87480 次 |
| 最近记录: |