如何在谷歌地图事件中访问此对象

时间:2013-04-23 09:48:09

标签: google-maps google-maps-api-3

我正在从Google Maps V2升级到V3。我坚持这个功能。在添加事件监听器时,我必须将此对象传递给该函数。我无法做到这一点。

示例:

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中如果我访问此对象,则引用新对象。

请帮忙。

由于

1 个答案:

答案 0 :(得分:0)

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;
        }
    }
}