Google Maps v3 API获取路由持续时间失败

Google Maps v3 API getting route duration fails

本文关键字:路由 持续时间 失败 获取 API Maps v3 Google      更新时间:2023-09-26

你能解释一下为什么我在这段代码中没有得到google.maps.DirectionsStatus.OK吗?

function measureDistance(m1, m2) {
    var wp = new Array();
    var lstart = new google.maps.LatLng(markers[m1].position);
    var lend = new google.maps.LatLng(markers[m2].position);
    var request = {
        origin : lstart,
        destination : lend,
        waypoints : [],
        optimizeWaypoints: true,
        travelMode : google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status== google.maps.DirectionsStatus.OK) {
            response.routes[0].legs[0].duration.text;
        } else {
            document.getElementById('locationD').value = "drawRouteBad";
        }
    });
}

Markers是一个全局字典,包含来自地图的标记。directionsService是全局定义的(同样的directionsService在另一个函数中为我工作)。问题是,由于某种原因,我不能得到正确的响应(我有drawRouteBad在我的网站上的一个字段)。

问题是,我不需要创建新的LatLng对象。里面是什么标记[]。起始和结束位置足够。

 var lstart = markers[m1].position;
 var lend = markers[m2].position;