markerClusterer on click zoom

末鹿安然 提交于 2019-12-03 01:16:54

There has been an update to the MarkerClusterer source code, allowing much easier access to the click event:

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

where 'markerCluster' ist the MarkerCluster object. Inside the function you may also access

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

I use this to switch to a different map type, as I use a custom tile set for easier overview on lower zoom levels:

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)

Regards Jack

I modified the clusterclick event as suggested:

/**
* 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);

 }
};

It works great! Thanks a lot

Natacha Beaugeais

You can do this without modifying the source code by using a listener on the clusterclick markerClusterer event:

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. I set zoomOnClick=false to have finer control of the map zooming behaviour to control the zoom amount and zoom location each click triggers.

It appears the API will only let you toggle the zoom functionality

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

So you will have to edit the source, it appears to be on line 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());
  }
};

If anyone needs to write this function in coffeescript I merged the top answer and the marked answer into one code snippet.

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

This code check is zoom on click is set. If it is zoom in to max zoom plus one, and center on the cluster. Very simple code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!