1

我正在使用此处找到的代码: Integrating Spiderfier JS into markerClusterer V3 以在单击 MarkerCluster 创建包含相同位置的点的集群时以完全相同的 long/lat 分解多标记以限制缩放级别。

现场示例在这里: http: //www.adultlearnersfestival.com/newsite/yourarea/map.html

但是,我在 Firebug 中遇到错误:

Error: TypeError: markers is undefined 

并且无法弄清楚是什么原因造成的。具体代码为:

var minClusterZoom = 14;
mc.setMaxZoom(minClusterZoom);
gm.event.addListener(mc, 'clusterclick', function(cluster) {
  map.fitBounds(cluster.getBounds()); // Fit the bounds of the cluster clicked on
  if( map.getZoom() > minClusterZoom+1 ) // If zoomed in past 15 (first level without clustering), zoom out to 15
  map.setZoom(minClusterZoom+1);
});

非常感谢任何帮助。- 汤姆

4

3 回答 3

1

我在这里建议了一种不同的方法: markerClusterer on click zoom并编辑了 MarkerClusterer 源,如下所示

由此

/**
 * Triggers the clusterclick event and zoom's if the option is set.
 */
ClusterIcon.prototype.triggerClusterClick = function() {
  var markerClusterer = this.cluster_.getMarkerClusterer();

  // Trigger the clusterclick event.
  google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_);

  if (markerClusterer.isZoomOnClick()) {
    // Zoom into the cluster.
    this.map_.fitBounds(this.cluster_.getBounds());
  }
};

对此

/**
 * Triggers the clusterclick event and zoom's if the option is set.
 */
ClusterIcon.prototype.triggerClusterClick = function() {
  var markerClusterer = this.cluster_.getMarkerClusterer();

  // Trigger the clusterclick event.
  google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_);

  if (markerClusterer.isZoomOnClick()) {
    // Zoom into the cluster.
    this.map_.fitBounds(this.cluster_.getBounds());

    // modified zoom in function
        if( this.map_.getZoom() > markerClusterer.getMaxZoom()+1 )
          this.map_.setZoom(markerClusterer.getMaxZoom()+1);
  }
};
于 2012-10-17T12:37:58.337 回答
0

在我看来,MarkerClusterer 中有一个错误。在 for 循环中的这个函数内部,标记是未定义的,这意味着 this.getMarkers() 返回未定义,在我看来这是错误的:

/**
 * Returns the bounds of the cluster.
 *
 * @return {google.maps.LatLngBounds} the cluster bounds.
 */
Cluster.prototype.getBounds = function() {
  var bounds = new google.maps.LatLngBounds(this.center_, this.center_);
  var markers = this.getMarkers();
  for (var i = 0, marker; marker = markers[i]; i++) {
    bounds.extend(marker.getPosition());
  }
  return bounds;
};

可能应该类似于(未测试):

/**
 * Returns the bounds of the cluster.
 *
 * @return {google.maps.LatLngBounds} the cluster bounds.
 */
Cluster.prototype.getBounds = function() {
  var bounds = new google.maps.LatLngBounds(this.center_, this.center_);
  var markers = this.getMarkers();
  if (markers && markers.length) 
  {
    for (var i = 0; i < markers.length; i++) {
      bounds.extend(markers[i].getPosition());
    }
  } 
  return bounds;
};

使用 MarkerClustererPlus 工作

于 2012-10-17T01:15:26.697 回答
0

我解决了改变这个的问题:

Cluster.prototype.getBounds = function() {
  var bounds = new google.maps.LatLngBounds(this.center_, this.center_);
  var markers = this.getMarkers();
  for (var i = 0, marker; marker = markers[i]; i++) {
    bounds.extend(marker.getPosition());
  }
  return bounds;
};

对此:

Cluster.prototype.getBounds = function() {
  var bounds = new google.maps.LatLngBounds(this.center_, this.center_);
  var markers = this.getMarkers();
  var minZoom =10 
  mc.setMaxZoom(minZoom);//The maximum zoom level that a marker can be part of a cluster  
  for (var i = 0; i < markers.length; i++) {
    bounds.extend(marker.getPosition());//Extends this bounds to contain the given point.
  }  
  if( map.getZoom() > minZoom+1 ){// If zoomed in past 11, the first level without clustering, zoom out to 11.
  map.setZoom(minZoom+1);
  } 
  return bounds;

};
于 2016-06-13T16:57:24.897 回答