2011-03-30 57 views
0

我想封裝谷歌地圖多邊形(google.maps.Polygon)和MAP(google.maps.Map)成JavaScript對象和處理兩個多邊形和映射中的某些事件。下面是一些代碼包封物谷歌地圖V.3事件監聽對象的方法

function Landmark(map, polygon) { 
    this.map = map; 
    this.polygon = polygon; 

    // Add a listener for the click event 
    google.maps.event.addListener(this.map, 'click', this.addPoint); 
    google.maps.event.addListener(this.polygon, 'click', this.addPoint); 

    addPoint = function (event) { 
     alert("xxx"); 
    } 
} 

我使用調用的函數:

var myLatLng = new google.maps.LatLng(24.886436490787712, -70.2685546875); 
    var myOptions = { 
         zoom: 5, 
         center: myLatLng, 
         mapTypeId: google.maps.MapTypeId.TERRAIN 
        }; 

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

    polygonInTest = new google.maps.Polygon({ 
               strokeColor: "#FF0000", 
               strokeOpacity: 0.8, 
               strokeWeight: 2, 
               fillColor: "#FF0000", 
               fillOpacity: 0.35 
              }); 

    polygonInTest.setMap(map); 

    var landmark = new Landmark(map, polygonInTest); 

但是,當我觸發事件,通過點擊地圖,我從螢火蟲這個錯誤:

f.e is undefined 
[Break On This Error] function de(a,b){var c,d=a.__e3_||{};i...n"+b]=c,c=new ce(a,b,c,3));return c}; 

任何人都可以指出我在哪裏做錯了,並提出一些建議?任何評論或幫助表示讚賞。

在此先感謝。

回答

0

我最好的猜測是,當你創建一個事件偵聽器

google.maps.event.addListener(this.map, 'click', this.addPoint); 

處理程序尚未確定(即你定義addPoint更高版本),所以在addListener()通話時間this.addPointundefined。而GMaps後來食堂在時機成熟時對要調用的處理程序。

至少這就是我通過查看你的代碼意識到自己有問題的原因:)