[英]Undefined is not a function in Google Maps API
I'm building an class to render maps using the Google Maps API. 我正在构建一个使用Google Maps API渲染地图的类。
This is my class 这是我的班级
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);
};
When I use this class, a received an error, in this line: 当我使用这个类时,收到一个错误,在这一行:
this.map.setMap(this.directions_service);
The error is: Uncaught Typeerror: undefined is not a function
错误是:
Uncaught Typeerror: undefined is not a function
But, I test the this.map
variable with typeof()
, and returns 'object'
. 但是,我使用
typeof()
测试this.map
变量,并返回'object'
。
The this.div_map
is set normally, also, the initial map in rendered. this.div_map
也正常设置,也就是渲染中的初始地图。
I'm doing something wrong? 我做错了什么?
Object map has no method 'setMap'. 对象映射没有方法'setMap'。 But DirectionRenderer has.
但DirectionRenderer有。 You have to change that line to
您必须将该行更改为
this.directions_service.setMap(this.map)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.