【发布时间】:2010-11-05 18:52:01
【问题描述】:
我遇到的所有使用 Google Maps API 的示例似乎都显示了某种地图。当您要求从 A 到 B 的道路描述时,我想将有关估计的汽车旅行时间的数据合并到站点中。而且只有那个数据。是否可以不为最终访问者加载地图?
【问题讨论】:
标签: api google-maps travel-time
我遇到的所有使用 Google Maps API 的示例似乎都显示了某种地图。当您要求从 A 到 B 的道路描述时,我想将有关估计的汽车旅行时间的数据合并到站点中。而且只有那个数据。是否可以不为最终访问者加载地图?
【问题讨论】:
标签: api google-maps travel-time
仅当满足以下所有条件时才返回流量持续时间:
请求包含一个离开时间参数。 该请求包括有效的 API 密钥或有效的 Google Maps APIs Premium Plan 客户端 ID 和签名。 交通状况可用于请求的路线。 模式参数设置为驱动。
至少从 2019 年 5 月开始,您现在可以在有或没有地图的情况下显示行驶时间。
您可以在 Google 地图上显示距离矩阵 API 结果,也可以不使用地图。如果您想在地图上显示距离矩阵 API 结果,那么这些结果必须显示在 Google 地图上。禁止在非 Google 地图的地图上使用距离矩阵 API 数据。
来源:Distance Matrix usage limits,另请查看Google Maps Terms & Conditions
请注意,Mapquest 不会根据实际交通情况显示行驶时间。
【讨论】:
我为此苦苦挣扎了很长时间,但最终通过 AJAX 调用解决了它(确保您已链接到 jQuery 来执行此操作)。
//Pass parameters to a function so your user can choose their travel
locations
function getCommuteTime(departLocation, arriveLocation) {
//Put your API call here with the parameters passed into it
var queryURL =
"https://maps.googleapis.com/maps/api/distancematrix/json?
units=imperial&origins=" + departLocation + "&destinations=" +
arriveLocation + "&key=YOUR_API_KEY"
//Ajax retrieves data from the API
$.ajax({
url: queryURL,
method: "GET"
}).then(function(response) {
//Navigate through the given object to the data you want (in this
case, commute time)
var commuteTime = response.rows[0].elements[0].duration.text;
console.log(commuteTime)
});
}
//This is where your user can give you the data you need
$("#addRoute").on("click", function(event) {
event.preventDefault();
var departLocation = $("#departInput").val().trim();
var arriveLocation = $("#arriveInput").val().trim();
//call the above function
getCommuteTime(departLocation, arriveLocation);
});
【讨论】:
上述答案与Google API terms of service违反,因此,不正确。
Google Maps API 仅允许您在参考显示给用户的 Google 地图时计算行程时间。
如果您不向服务的最终用户显示 Google 地图,则无法使用 API。
更新:
这里的任何答案,如果没有在谷歌地图上显示最终结果,都违反了上述服务条款,最终是不正确的。
【讨论】:
是的,这绝对可以使用 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 ();
然后执行加载请求以解析方向(我使用了两个纬度和经度作为起点和终点,但您也可以在此处使用地址):
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:
GEvent.addListener(directions, "load", function() {
$('log').innerHTML = directions.getDuration ().seconds + " seconds";
});
您可以找到whole example here 和JavaScript here。您可以查看Google Maps Documentation 以获取有关可以为 GDirections 对象提供的选项的更多信息(例如步行距离等...)。您还应该收听地理编码失败的 error 事件。
【讨论】:
来自 google.maps.DirectionsRenderer
使用距离:
directionsDisplay.directions.routes[0].legs[0].distance.text
使用时长:
directionsDisplay.directions.routes[0].legs[0].duration.text
【讨论】:
备案:目前(2015 年)GDirections、GLatLng、GEvent 等已弃用。
您可以使用google.maps.DirectionsService class (Google Maps JavaScript API V3)
在下面的sn-p中,location和target都是包含经纬度坐标的对象。
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 + ')' );
}
} );
【讨论】: