我使用谷歌地图API遇到的所有例子似乎都显示了某种地图。我希望在您向A站点询问道路描述时,将他们提供给您的汽车估计行驶时间的数据纳入到站点中。只有那些数据。没有为最终访问者加载地图是否可能?使用谷歌地图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);
然后,你需要监听负载事件,这样你可以调用getDuration一旦方向已经得到解决:
GEvent.addListener(directions, "load", function() {
$('log').innerHTML = directions.getDuration().seconds + " seconds";
});
您可以找到whole example here和JavaScript here。你可以检查Google Maps Documentation了解更多关于你可以给GDirections对象的选项信息(比如步行距离等)。您还应该听取错误事件的地理编码失败。
上述答案不正确 - 违反Google API服务条款。
Google Maps API只允许您计算旅行时间,如果它是针对显示给用户的Google地图引用的。
如果您没有向服务的最终用户显示Google地图,则无法使用该API。
伪代码:
DirectionsService .route({A,B})//得到这里的DirectionsResult在
检查距离和持续时间(行程时间)DirectionsLeg .//from的DirectionsResult .legs
没有航点的路线将包含一个DirectionsLeg,所以这就是你想要的。
根据记录:在这个时候(2015年)GDirections对象,的GLatLng,GEVENT等都是弃用。
您可以使用google.maps.DirectionsService class(谷歌地图的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 + ')');
}
});
从google.maps.DirectionsRenderer使用
距离:
使用directionsDisplay.directions.routes[0].legs[0].distance.text
时间:
directionsDisplay.directions.routes[0].legs[0].duration.text
你必须在2016年谷歌地图距离矩阵API https://developers.google.com/maps/documentation/distance-matrix/get-api-key#key
您可以使用R包gmapsdistance。它确实是你想要的。它计算起源和目的地向量之间的距离/时间矩阵,并为您提供一系列选项(运输方式,出发时间等等)。
- 第一去这里: https://developers.google.com/maps/documentation/distance-matrix/start 你需要:创建或选择一个项目,启用API,并得到一个API密钥 只需点击“一键搞定”,并假设你有一个Gmail帐户,就能让一切顺利(或者在登录到您的gmail帐户后进入console.developer.google.com,并创建一个应用程序并启用该API并在那里获取API密钥)。
- 现在去https://developers.google.com/maps/documentation/distance-matrix/intro并按照细节。有几件事需要注意:使用这些: & mode = driving & departure_time = now & traffic_model =悲观如果您希望当前流量影响您的驾驶时间。我还使用&单位=帝国
在流量的持续时间仅返回如果以下所有条件都为真:
该请求包括DEPARTURE_TIME参数。 该请求包含有效的API密钥或有效的Google Maps API Premium计划客户端ID和签名。 交通状况可用于请求的路线。 模式参数设置为驾驶。
是的,你必须显示一个谷歌地图的驱动时间“谷歌地图距离矩阵API可能只能与在Google地图上显示结果一起使用。禁止使用谷歌地图距离矩阵API数据不显示谷歌地图“
一定要结账。 https://developers.google.com/maps/documentation/distance-matrix/usage-limits 和 https://developers.google.com/maps/terms 对于T & C'S。
请注意,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);
});
现在我们假设我有一个有50个点的kml文件。我希望得到所有这些点之间的旅行时间,你应该怎么做? – dassouki 2009-09-18 14:52:08
该链接不起作用了! – Prachi 2016-05-05 11:48:13
这似乎在理想的交通状况下(忽略当前状况)产生驾驶时间。无论出发时间如何,结果都是相同的。 – user3667349 2016-06-11 00:56:50