MarkerClusterer при масштабировании клика
Я только что добавил MarkerClusterer на мою карту google. Он отлично работает.
Мне просто интересно, есть ли способ настроить поведение масштабирования при щелчке кластера. Я хотел бы изменить уровень масштабирования, если это возможно.
Есть ли способ достичь этого?
Спасибо
Ответы
Ответ 1
Я изменил событие clusterclick, как было предложено:
/**
* Triggers the clusterclick event and zoom 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);
}
};
Он отлично работает! Большое спасибо
Ответ 2
Обновлен исходный код 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)
Отношения
Джек
Ответ 3
Это можно сделать без изменения исходного кода, используя прослушиватель события 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);
});
то есть. Я установил zoomOnClick = false, чтобы лучше контролировать масштабирование карты, чтобы контролировать величину масштабирования и положение масштабирования при каждом нажатии.
Ответ 4
Похоже, что API позволит вам переключать функцию масштабирования
http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html
Итак, вам нужно будет отредактировать источник, он, похоже, находится в строке 1055
/**
* Triggers the clusterclick event and zoom 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());
}
};
Ответ 5
Если кому-то нужно написать эту функцию в 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
Эта проверка кода - это увеличение по щелчку. Если он масштабируется до максимального увеличения плюс один и центрируется на кластере. Очень простой код.