2010-11-24 50 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

回答