2013-04-23 58 views
1

我正在從Google Maps V2升級到V3。我堅持這個功能。在添加事件監聽器時,我必須將該對象傳遞給該函數。我無法這樣做。如何在Google地圖事件中訪問此對象

例子:

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

我想訪問enableZoneDraw的多個對象中toggleZoneDraw,但在toggleZoneDraw如果我訪問該對象時,它是指新的對象。

請幫忙。

感謝

回答

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; 
     } 
    } 
} 
相關問題