8

我正在尝试稍微偏移由 Google Maps Markerclusterer (V3) 创建的集群图标。没有修改现有代码,我找不到这样做的方法。有人有想法吗?

您可以在其中提供自定义图像 URL 的 Styles 对象接受锚属性,但这是为了抵消生成的标记项计数。

谢谢!

4

5 回答 5

5

正确的方法是像这样调整anchorIcon属性:

var clusterStyles = [
{
    height: 64,
    width: 53,
    anchorIcon: [20, 140]
},
{
    height: 64,
    width: 53,
    anchorIcon: [20, 140]
},
{
    height: 64,
    width: 53,
    anchorIcon: [20, 140]
}
];

var mcOptions = {
    styles: clusterStyles
};

var markerCluster = new MarkerClusterer(map, markers, mcOptions);
于 2012-11-23T09:52:27.933 回答
4

接受的答案对我来说效果不够好 - 向图标图像添加透明空间可以改变单击和悬停事件的行为方式,因为对象的大小增加了。

我会使用该anchorIcon属性,但它仅在Marker Clusterer Plus中可用,而不是其他Marker Clusterer插件(我正在使用)。

对于那些特别想使用 Marker Clusterer 的人-您可以覆盖ClusterIcon.prototype.getPosFromLatLng_. 该ClusterIcon对象是全局的,因此我们可以在任何脚本文件的顶层对其进行修改,而不会弄乱插件的源代码。

这会将标记锚定到图标的底部:

ClusterIcon.prototype.getPosFromLatLng_ = function (latlng) {
  var pos = this.getProjection().fromLatLngToDivPixel(latlng);
  pos.x -= parseInt(this.width_ / 2);
  pos.y -= parseInt(this.height_);
  return pos;
};
于 2013-12-03T23:26:04.690 回答
1

我通过修改以下两个函数更改了marcerclusterer.js的代码以支持anchorText参数:

/**
 * Sets the icon to the the styles.
 */
ClusterIcon.prototype.useStyle = function() {
  var index = Math.max(0, this.sums_.index - 1);
  index = Math.min(this.styles_.length - 1, index);
  var style = this.styles_[index];
  this.url_ = style['url'];
  this.height_ = style['height'];
  this.width_ = style['width'];
  this.textColor_ = style['textColor'];
  this.anchor_ = style['anchor'];
  this.anchorText_ = style['anchorText']; //added to support anchorText parameter by Frane Poljak, Locastic
  this.textSize_ = style['textSize'];
  this.backgroundPosition_ = style['backgroundPosition'];
};


/**
 * Adding the cluster icon to the dom.
 * @ignore
 */
ClusterIcon.prototype.onAdd = function() {
  this.div_ = document.createElement('DIV');
  if (this.visible_) {
    var pos = this.getPosFromLatLng_(this.center_);
    this.div_.style.cssText = this.createCss(pos);

      ////added to support anchorText parameter by Frane Poljak, Locastic

      if (typeof this.anchorText_ === 'object' && typeof this.anchorText_[0] === 'number' && typeof this.anchorText_[1] === 'number') {
          this.div_.innerHTML = '<span style="position:relative;top:' + String(this.anchorText_[0]) + 'px;left:' + String(this.anchorText_[1]) + 'px;">' + this.sums_.text + '</span>'
      } else this.div_.innerHTML = this.sums_.text;
  } 

  var panes = this.getPanes();
  panes.overlayMouseTarget.appendChild(this.div_);

  var that = this;
  google.maps.event.addDomListener(this.div_, 'click', function() {
    that.triggerClusterClick();
  });
};
于 2015-08-26T12:37:27.500 回答
0

您可以在群集图标的 PNG 的一侧添加一些透明空间,以便您希望居中的图标部分实际上也在您的 PNG 中居中。这不应使图像的权重增加超过几个字节。

于 2011-04-08T13:52:26.237 回答
0

anchor / anchorIcon / anchorText 属性对我不起作用......所以我做了一种解决方法:

我使用setCalculator()函数来设置集群文本:

https://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html

当我设置集群文本属性时,我用 包装了值<span>,如下所示:

markerCluster.setCalculator(function (markers) {
   return {
      text: '<span class="myClass">' + value+ '</span>',
      index: index                   
   };
});

现在您可以使用“.myClass”控制集群标签位置:

span.myClass{
   position: relative;
   top: -15px;
   .....
}
于 2014-07-03T11:09:34.363 回答