未定义不是Google Maps API中的功能

时间:2014-04-30 15:58:16

标签: javascript google-maps

我正在构建一个使用Google Maps API渲染地图的课程。

这是我的班级

function Map() {
}

Map.prototype.map = null;
Map.prototype.map_options = null;
Map.prototype.div_map = null;
Map.prototype.directions_service = null;
Map.prototype.center = null;
Map.prototype.points = null;

Map.prototype.start = function() {

    this.map_options = {
        zoom: 14,
        center: this.center,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        streetViewControl: false
    };

    this.map = new google.maps.Map(document.getElementById(this.div_map), this.map_options);

    this.directions_service = new google.maps.DirectionsRenderer();

    this.directions_service.setOptions({
        preserveViewport: true,
        suppressInfoWindows: true,
        polylineOptions: {
            strokeWeight: 2,
            strokeOpacity: 0.8,
            strokeColor: "blue"
        }
    });

    this.map.setMap(this.directions_service);
};

当我使用这个类时,收到一个错误,在这一行:

this.map.setMap(this.directions_service);

错误是:Uncaught Typeerror: undefined is not a function

但是,我使用this.map测试typeof()变量,然后返回'object'

this.div_map正常设置,也就是渲染中的初始地图。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

对象地图没有方法' setMap'。但DirectionRenderer有。您必须将该行更改为

this.directions_service.setMap(this.map)