26

I am currently using markercluster plugin with jquery ui maps.

I have two arrays one of all markers (called markers) and one of markers that match search criteria (called current_markers). These are fitered from the first array.

I then draw the current_markers on screen.

I am finding however that the markerclusterer library is not updating based on this change.

So how can I update the markerclusterer?

Is it possible to assign the markerclusterer to a variable and call an update function?

4

5 に答える 5

57

はい、できます。

マップの作成

次のような MarkerClusterer オブジェクトを作成したと仮定します。

var center = new google.maps.LatLng(10, 20);
var map = new google.maps.Map(document.getElementById('map'), { zoom: 6, center: center, mapTypeId: google.maps.MapTypeId.ROADMAP });
var markerClusterer = new MarkerClusterer(map);

マーカーの追加

次のように、複数のマーカーを追加できます。

var markers = []
var marker = new google.maps.Marker({position: center});
markers.push(marker);
markerClusterer.addMarkers(markers);

ここでは 1 つだけ追加したことに注意してください。

すべてのマーカーを削除する

次に、次のように clearMarkers を使用してすべてのマーカーをクリアできます。

markerClusterer.clearMarkers();
markers = [];

整頓のために、ここでマーカー配列の設定も解除したことに注意してください。

ドキュメント

利用可能なすべてのメソッドに関する完全なドキュメントは、次の場所にあります。

https://googlemaps.github.io/js-marker-clusterer/docs/reference.html

更新されたリンク: https://googlemaps.github.io/js-markerclustererplus/classes/markerclusterer.html#clearmarkers

これは賢明で比較的完全な API です。

于 2014-06-24T09:27:29.697 に答える
17

マーカー オブジェクトを var に保存してから、次のようにマップを設定解除する必要があります。

var markerCluster = new MarkerClusterer(map, markers);
/// ... later on
markerCluster.setMap(null);

new MarkerClustererこれを行った後、新しいマーカーでa を初期化できます

アップデート

Google マップの ui プラグインを使用しているため、ここにいくつかの追加コードがあります。クラスのボタンにもクリックを追加しましたreset_markerclusterもちろん、これはそれを使用してマップを呼び出す方法を示すためのものです

var _map, _markerCluster;

$(function() {
  $('#map_canvas').gmap().bind('init', function(event, map) { 
    _map = map; // at this point you can call _map whenever you need to call the map

    // build up your markers here ...

    _markerCluster = new MarkerClusterer(_map, markers);  // you could also use map instead of _map here cause it's still present in this function
  });

  $("button.reset_markercluster").click(function(e) {
    e.preventDefault();
    _markerCluster.setMap(null);  // remove's the previous added markerCluster

    // rebuild you markers here ...

    _markerCluster = new MarkerClusterer(_map, newMarkers);

  });
});
于 2011-11-22T16:07:43.073 に答える
12

markerCluster オブジェクトから clearMarkers() メソッドを使用することをお勧めします。

http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html 更新されたリファレンス: https://googlemaps.github.io/js-markerclustererplus/classes/markerclusterer. html#クリアマーカー

于 2013-08-31T19:58:08.343 に答える
2

marker-clusterer-plusには次のremoveMarkersメソッドがあります。

markerCluster.removeMarkers(markers);
于 2016-05-20T00:49:26.200 に答える