markerClusterer при нажатии zoom

Я только что добавил MarkerClusterer на мою карту google. Он отлично работает.

Мне просто интересно, есть ли способ настроить поведение масштабирования при нажатии на кластер. Я хотел бы изменить уровень масштабирования, если это возможно.

есть ли способ достичь этого?

спасибо

5 ответов


было обновление исходного кода 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)

С уважением Джек!--4-->


вы можете сделать это без изменения исходного кода с помощью прослушивателя события 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);
});

ie. Я установил zoomOnClick=false, чтобы иметь более точный контроль поведения масштабирования карты для управления величиной масштабирования и местоположением масштабирования.


Я изменил событие 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);

 }
};

Он отлично работает! Большое спасибо


похоже, 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());
  }
};

Если кому-то нужно написать эту функцию в 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

эта проверка кода zoom on click установлена. Если это увеличение до максимального увеличения плюс один, и центр на кластере. Очень простой код.