Google maps 如何在google maps事件中访问此对象
我正在从谷歌地图V2升级到V3。我被这个功能卡住了。在添加事件侦听器时,我必须将this对象传递给函数。我无法解释这一点 例如:Google maps 如何在google maps事件中访问此对象,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我正在从谷歌地图V2升级到V3。我被这个功能卡住了。在添加事件侦听器时,我必须将this对象传递给函数。我无法解释这一点 例如: Namespace.mapWrapper.prototype.enableZoneDraw = function(callback) { if (!this.isDrawing) { //this.clickListener = GEvent.bind(this.api, 'click', this, this.toggleZoneDraw);
Namespace.mapWrapper.prototype.enableZoneDraw = function(callback) {
if (!this.isDrawing) {
//this.clickListener = GEvent.bind(this.api, 'click', this, this.toggleZoneDraw);
this.clickListener = google.maps.event.addListener(this.api, 'click', this.toggleZoneDraw); //this.api is map object
if (callback) {
this.drawEndCallback = callback;
}
}
}
Namespace.mapWrapper.prototype.toggleZoneDraw = function(event) {
// Start drawing zone
if (!this.isDrawing) {
if(event.latLng){
this.zoneCenter = event.latLng;
this.isDrawing = true;
this.drawListener = google.maps.event.addListener(this.api, 'mousemove', another_function);
}
} else {
this.isDrawing = false;
google.maps.event.removeListener(this.drawListener);
google.maps.event.removeListener(this.clickListener);
}
}
我想在toggleZoneDraw中访问更多enableZoneDraw对象,但在toggleZoneDraw中,如果我访问此对象,它将引用新对象
请帮忙
谢谢
Namespace.mapWrapper.prototype.enableZoneDraw = function(callback) {
if (!this.isDrawing) {
//create a reference to the current this object
var obj = this;
this.clickListener = google.maps.event.addListener(
this.api, //this.api is map object
'click',
//use a closure to maintain our reference to this object
function (evt) {
obj.toggleZoneDraw(evt);
}
);
if (callback) {
this.drawEndCallback = callback;
}
}
}