markerClusterer点击缩放

时间:2011-04-19 01:14:36

标签: javascript google-maps zoom google-maps-markers markerclusterer

我刚刚在我的谷歌地图上添加了一个MarkerClusterer。它完美无缺。

我只是想知道在点击群集时是否有任何调整放大行为的方法。我想尽可能改变缩放级别。

有没有办法实现这个目标?

由于

5 个答案:

答案 0 :(得分:53)

对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)

此致 千斤顶

答案 1 :(得分:8)

您可以在不修改源代码的情况下通过在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(cluster.getCenter());
    map.setZoom(map.getZoom()+1);
});

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

答案 2 :(得分:6)

我按照建议修改了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);

 }
};

效果很好!非常感谢

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

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

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