【发布时间】:2019-12-13 02:18:27
【问题描述】:
我正在尝试使用MarkerClusterer 对地图上的标记进行聚类。 问题是我没有使用默认标记 (google.maps.Marker),而是使用了一个自定义类,它阻碍了 google.maps.OverlayView。 不幸的是,该库似乎是在使用基本标记的情况下开发的,实际上我得到了错误,因为我的类没有实现 google.maps.Marker 中定义的方法。 是否可以通过保留我的自定义标记来使用 MarkerClusterer?
编辑:这比我预期的要容易得多,我通过在我的自定义类中实现 2 个方法来解决:
setVisible() 和 getPosition()
为了帮助别人以下是我的完整接口(没有完整实现):
BFPushpin = function(config)
{
this.setMap(config.map);
this.set("position", config.position);
// other settings...
};
// my class extends google.maps.OverlayView
BFPushpin.prototype = new google.maps.OverlayView();
BFPushpin.prototype.getBounds = function()
{
return new google.maps.LatLngBounds(this.position, this.position);
};
BFPushpin.prototype.getPoint = function()
{
var bounds = this.getBounds();
var projection = this.getProjection();
var sw = projection.fromLatLngToDivPixel(bounds.getSouthWest());
var ne = projection.fromLatLngToDivPixel(bounds.getNorthEast());
return new google.maps.Point(sw.x, ne.y);
};
BFPushpin.prototype.getSuperContainer = function()
{
var panes = this.getPanes();
return jQuery(panes ? panes.overlayImage : "");
};
BFPushpin.prototype.getContainer = function()
{
// return inner container
};
BFPushpin.prototype._generatePopupContent = function()
{
// return markup for the popupwindow
};
BFPushpin.prototype._addListeners = function()
{
// add handlers for the pushpin
};
BFPushpin.prototype.onAdd = function()
{
// customize content here
};
BFPushpin.prototype.onRemove = function()
{
// remove pin container here
};
BFPushpin.prototype.draw = function()
{
// set display style here
};
BFPushpin.prototype.setVisible = function(visible)
{
// set display block or hidden
};
BFPushpin.prototype.getPosition = function()
{
return this.position;
};
【问题讨论】:
-
我对你的解决方案有点困惑。我正在尝试做类似的事情,我有一个从 OverlayView “继承”的自定义叠加层,并且我通过简单地在绘图函数中枚举我自己制作的对象列表来在其中绘制标记作为标记。那么,您如何协调此处“标记”的使用以使集群发挥作用?
-
您是否为每个标记渲染一个完整的叠加层?
标签: javascript google-maps google-maps-api-3