markerclusterer:群集图标的锚点偏移量

时间:2011-02-01 18:57:16

标签: google-maps markerclusterer

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

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

谢谢!

5 个答案:

答案 0 :(得分:4)

正确的方法是调整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);

答案 1 :(得分:3)

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

另一方面,anchorIcon属性仅存在于Marker Clusterer Plus中,而不存在于其他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;
};

答案 2 :(得分: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();
  });
};

答案 3 :(得分:0)

您可以在群集图标的PNG的一侧添加一些透明空间,这样您想要居中的图标部分实际上也会在您的PNG中居中。这不应该增加图像的重量超过几个字节。

答案 4 :(得分: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;
   .....
}