markerClusterer点击缩放

Ale*_*and 25 javascript google-maps zoom google-maps-markers markerclusterer

我刚刚在谷歌地图上添加了MarkerClusterer.它工作得很好.

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

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

谢谢

小智 57

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

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

其中'markerCluster'是MarkerCluster对象.在功能内部,您也可以访问

cluster.getCenter();
cluster.getMarkers();
cluster.getSize();
Run Code Online (Sandbox Code Playgroud)

我使用它来切换到不同的地图类型,因为我使用自定义图块集来更容易地在较低缩放级别上进行概述:

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)
Run Code Online (Sandbox Code Playgroud)

问候杰克

  • 这是在某处记录的吗? (2认同)

Nat*_*ais 9

您可以在不修改源代码的情况下通过在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);
});
Run Code Online (Sandbox Code Playgroud)

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


Ale*_*and 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);

 }
};
Run Code Online (Sandbox Code Playgroud)

它很棒!非常感谢