2010-11-24 48 views
5

我在嘗試轉換非常有用的LabeledMarker class to Google Maps API V3。 我在V2地圖中使用它,但我想在V3地圖中使用類似的東西。 我讀了Mike's article關於擴展GMarker。Google Maps API V3 - 子類google.maps.Marker:如何調用父級方法?

我試圖做google.maps.Marker相同,但我遇到了一些問題。

這裏是我的代碼非常簡單:

function LabeledMarker(opts) { // constructor 
    google.maps.Marker.apply(this, arguments); 
} 

LabeledMarker.prototype = new google.maps.Marker(); 

LabeledMarker.prototype.onAdd = function() { 
    alert('onAdd1'); 
    google.maps.Marker.prototype.onAdd.apply(this, arguments);  
    alert('onAdd2'); 
} 

LabeledMarker.prototype.draw = function() { 
    alert('draw1'); 
    google.maps.Marker.prototype.draw.apply(this, arguments); 
    alert('draw2'); 
} 

LabeledMarker.prototype.onRemove = function() { 
    alert('onRemove1'); 
    google.maps.Marker.prototype.onRemove.apply(this, arguments); 
    alert('onRemove2'); 
} 

以下是我把它叫做:

var point = new google.maps.LatLng(37, -59); 
var labeledMarker = new LabeledMarker({ 
    map: map, 
    position: point, 
    labelText: 'Label' 
}); 

這裏有一個網址:http://www.canamgroup.ws/GM.nsf/Map2?OpenPage

我的標記顯示在地圖上(所以我想我的構造函數成功地調用google.maps.Marker構造函數),但onAdd,draw和onRemove中的警報從不觸發(所以我假設我的方法不是成功地調用google.maps.Marker方法)。

這是Mike在V2中的做法(除了方法名稱不同)。 我想:

google.maps.Marker.prototype.draw.apply(this, arguments); 
google.maps.Marker.prototype.draw.apply(this); 
google.maps.Marker.prototype.draw.call(this); 
google.maps.Marker.prototype.draw.call(this, arguments); 

我剛剛開始編寫OO的Javascript,所以我可能失去了一些東西? 或者我可能需要在V3中做一些不同的事情? 有人可以幫忙嗎?

謝謝!

+0

我的問題應該更「如何覆蓋使用onAdd(),畫()和谷歌的onRemove()方法。 maps.Maker?」。 我使用「setTitle」方法進行了測試,並顯示了我的警報(以及所調用的父級方法): LabeledMarker.prototype.setTitle = function(title){alert('setTitle'); google.maps.Marker.prototype.setTitle.apply(this,arguments); } 這是否意味着google.maps.Marker沒有onAdd(),draw()和onRemove()方法? 標記是覆蓋圖,所以我想我可以重寫這些方法。 我想做一些不可能的事情嗎? 謝謝! – Canam 2010-11-24 15:39:43

回答