Qiita Conference 2025

けんちょん (@drken)

読者層の解像度を高めて、読まれる記事を書こう!

3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Maps APIのMarker Clusterers libraryを使用した際のcluster iconのclick event

Last updated at Posted at 2018-06-27

やりたいこと

cluster iconをクリックしたときの挙動を変えたい(デフォルトはクリックするとズームする)

コード

markerclusterer.js
/**
 * 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());
  }
};

ライブラリの上記がcluster iconがクリックされた時のイベント部分です。
markerClusterer.isZoomOnClickはmarkerClustererのオプションです。

Name Type Description
zoomOnClick boolean Whether the default behaviour of clicking on a cluster is to zoom into it.

zoomOnClickがtrueのときにズームされるみたいです。
単純に常にズームしないのであればzoomOnClick:falseにすれば良いです。
また、例えばズームレベルが9未満のときにのみズームさせるのであれば、 if (markerClusterer.isZoomOnClick()) {の部分に以下を追記すれば良いです。

markerclusterer.js
  if (markerClusterer.isZoomOnClick()) {
    if(this.map_.getZoom() < 9){
    // Zoom into the cluster.
    this.map_.fitBounds(this.cluster_.getBounds());
    }
  }

私の場合は、単にmapをドラッグしたいだけなのに、cluster iconのところでドラッグしてしまうと、ドラッグした後にズームしてしまう:pouting_cat:のをなんとかしたかったので、上記の部分を「ドラッグしていたらズームしない」というように変えました。

参考:Gmaps Marker Clusterer

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?