30

我刚刚在我的谷歌地图中添加了一个 MarkerClusterer。它工作得很好。

我只是想知道单击群集时是否有任何方法可以调整放大行为。如果可能,我想更改缩放级别。

有没有办法做到这一点?

谢谢

4

5 回答 5

60

MarkerClusterer 源代码已更新,允许更轻松地访问 click 事件:

google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
    // your code here
});

其中“markerCluster”是 MarkerCluster 对象。在该功能内,您还可以访问

cluster.getCenter();
cluster.getMarkers();
cluster.getSize();

我使用它来切换到不同的地图类型,因为我使用自定义图块集以便更轻松地查看较低缩放级别:

map.setCenter(cluster.getCenter()); // zoom to the cluster center
map.setMapTypeId(google.maps.MapTypeId.ROADMAP); // switch map type
map.setOptions(myMapOptions); // apply some other map options (optional)

问候杰克

于 2011-09-22T10:01:59.320 回答
11

您可以通过在 clusterclick markerClusterer 事件上使用侦听器来执行此操作,而无需修改源代码:

var mcOptions = {gridSize: 40, maxZoom: 16, zoomOnClick: false, minimumClusterSize: 2};
markerClusterer = new MarkerClusterer(map, markers, mcOptions);

google.maps.event.addListener(markerClusterer, 'clusterclick', function(cluster){
    map.setCenter(markerClusterer.getCenter());
    map.setZoom(map.getZoom()+1);
});

IE。我设置 zoomOnClick=false 以更好地控制地图缩放行为,以控制每次单击触发的缩放量和缩放位置。

于 2015-06-13T16:46:09.750 回答
8

我按照建议修改了 clusterclick 事件:

/**
* 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
this.map_.setZoom(markerClusterer.getMaxZoom()+1);

 }
};

效果很好!非常感谢

于 2011-04-19T15:21:04.283 回答
3

看起来 API 只会让你切换缩放功能

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

所以你必须编辑源代码,它似乎在第 1055 行

/**
 * 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());
  }
};
于 2011-04-19T04:21:00.580 回答
1

如果有人需要在 coffeescript 中编写此函数,我会将最佳答案和标记答案合并到一个代码片段中。

mcOptions =
  maxZoom: 16

markerCluster = new MarkerClusterer map, markers, mcOptions
# listener if a cluster is clicked
google.maps.event.addListener markerCluster, "clusterclick", (cluster) ->
  if markerCluster.isZoomOnClick() # default is true
    #get bounds of cluster
    map.fitBounds cluster.getBounds()
    #zoom in to max zoom plus one. 
    map.setZoom markerCluster.getMaxZoom() + 1

此代码检查设置为单击时放大。如果它放大到最大缩放加一,并以集群为中心。非常简单的代码。

于 2017-05-14T05:39:23.547 回答