5

sごとに異なるマーカーが必要でありmapType、それらをMarkerClustererにプッシュしています。

マーカーを次のように「非表示」にします。

cluster.set("map", null);
cluster.resetViewport();
cluster.redraw();

そして、それらを「表示」します。

cluster.set("map", MAP);
cluster.resetViewport();
cluster.redraw();

問題は、MarkerClustererが気に入らないように見えることset("map", null)です。エラーをスローしますTypeError: Object #<a MarkerClusterer> has no method 'remove'。適切な方法で表示/非表示にするにはどうすればよいですか?

4

5 に答える 5

6

Javascript API v3 では、次のように言うだけで十分です。

clusterer.setMap(null);

マップを既存のマップ オブジェクトに戻すと、クラスターが再び表示されます。

clusterer.setMap( this.map );

また、例のように、クラスタラーに「クラスター」という名前を付けないことをお勧めします。MarkerClusterer には Cluster オブジェクトが含まれています。これは実際にクラスター化されたマーカーであり、ClusterER 自体ではありません。

于 2012-03-22T10:48:51.930 に答える
6

クラスターをクリアするエレガントな方法

cluster.clearMarkers();
于 2012-09-26T07:15:18.380 に答える
2

より完全な解決策は次のとおりです。

.html に追加:

<div id="map-canvas-hidden" style="display:none"></div>
<div id="map-canvas-shown" style="width:500px; height:500px"></div>

.js に次を追加します。

MarkerClusterer.prototype.remove = function() { };
var HIDDEN_MAP = new google.maps.Map(document.getElementById("map-canvas-hidden"), {});
var gmap = new google.maps.Map(document.getElementById("map-canvas-shown"), {});

クラスターを表示するには:

    cluster.setMap(gmap);
    cluster.resetViewport();
    cluster.redraw();

クラスターを非表示にするには:

    cluster.setMap(HIDDEN_MAP);
    cluster.resetViewport();
    cluster.redraw();

最後に、markerclusterer.js に次のパッチを適用する必要がありました。

--- markerclusterer.js.orig 2013-12-06 18:02:32.887516000 +0100
+++ markerclusterer.js  2013-12-06 18:03:25.487516924 +0100
@@ -620,6 +620,7 @@
  */
 MarkerClusterer.prototype.getExtendedBounds = function(bounds) {
   var projection = this.getProjection();
+  if (!projection) return null;

   // Turn the bounds into latlng.
   var tr = new google.maps.LatLng(bounds.getNorthEast().lat(),
@@ -657,7 +658,7 @@
  * @private
  */
 MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) {
-  return bounds.contains(marker.getPosition());
+  return bounds ? bounds.contains(marker.getPosition()) : false;
 };

お役に立てれば

于 2013-12-06T17:15:20.267 に答える
0

私は少しモンキーパッチを当てて少しハックすることで、これを解決しようと奮闘しました。私はまだ明確な答えを待っていますが、これ私の問題の解決策なので、これも投稿しています:

MarkerClusterer.prototype.remove = function () {}

[..]

cluster.set("map", HIDDEN_MAP); // remove the clusterer
cluster.resetViewport();
cluster.redraw();
于 2010-10-31T18:40:45.020 に答える