如何解决“无法调用未定义的方法”错误?

时间:2012-04-20 11:56:50

标签: javascript google-maps-api-3

    function calcRoute() {
        var start = document.getElementById("start_").value;
        var end = document.getElementById("end_").value;
        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });
    }

在Chrome中提供错误消息:"未捕获的TypeError:无法调用方法' setDirections'未定义"。有人可以建议修复这个吗?感谢名单

1 个答案:

答案 0 :(得分:6)

您缺少(全局变量)

//var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({ 'map': map }); 

所以directionsDisplay未定义。