如何获得与谷歌地图API V3总行驶距离是多少? [英] How to get total driving distance with Google Maps API V3?

查看:162
本文介绍了如何获得与谷歌地图API V3总行驶距离是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经是能够得到这样的:

I used to be able to get it like this:

directionsService.route(directionsRequest, function(directionsResult, directionsStatus) {
    var directionsRenderer = new google.maps.DirectionsRenderer({
        directions: directionsResult,
        map: map
    });
    $('#distance').text(directionsResult.trips[0].routes[0].distance.text)
    $('#duration').text(directionsResult.trips[0].routes[0].duration.text)
})

但它看起来像他们改变<一href=\"http://$c$c.google.com/apis/maps/documentation/javascript/reference.html#DirectionsResult\">their对我API !貌似车次就不再出现,而路线只给你一堆的腿......我真的有遍历所有的腿,现在总结的距离?

But it looks like they changed their API on me! Looks like trips is no longer there, and routes only gives you a bunch of legs... do I really have to iterate over all the legs and sum up the distance now?

推荐答案

根据 Leniel的回答

var totalDistance = 0;
var totalDuration = 0;
var legs = directionsResult.routes[0].legs;
for(var i=0; i<legs.length; ++i) {
    totalDistance += legs[i].distance.value;
    totalDuration += legs[i].duration.value;
}
$('#distance').text(totalDistance);
$('#duration').text(totalDuration);

其实,这工作得很好过,如果没有任何航点:

Actually, this works just fine too, if you don't have any waypoints:

$('#distance').text(directionsResult.routes[0].legs[0].distance.text);
$('#duration').text(directionsResult.routes[0].legs[0].duration.text);

这篇关于如何获得与谷歌地图API V3总行驶距离是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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