Google MarkerClusterer: decluster markers below a certain zoom level?

后端 未结 3 661
余生分开走
余生分开走 2020-12-05 23:34

I\'m using Google MarkerClusterer. I\'d like to decluster all the markers whenever the map goes above zoom level 15.

There is a maxZoom setting in the

3条回答
  •  一整个雨季
    2020-12-06 00:15

    Setting the maxZoom level on this example, declusters all the markers for zoom level 8 and above.

    To reproduce:

    1. set Max zoom level to 7
    2. click refresh map
    3. change the zoom level to 0 (the furthest out)
    4. click the "+" on the zoom slider 8 times.

    The documentation for MarkerClustererPlus is a little clearer:

    maxZoom | number | The maximum zoom level at which clustering is enabled or null if clustering is to be enabled at all zoom levels. The default value is null.

    code snippet:

    var styles = [
      [{
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/people35.png',
        height: 35,
        width: 35,
        anchor: [16, 0],
        textColor: '#ff00ff',
        textSize: 10
      }, {
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/people45.png',
        height: 45,
        width: 45,
        anchor: [24, 0],
        textColor: '#ff0000',
        textSize: 11
      }, {
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/people55.png',
        height: 55,
        width: 55,
        anchor: [32, 0],
        textColor: '#ffffff',
        textSize: 12
      }],
      [{
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/conv30.png',
        height: 27,
        width: 30,
        anchor: [3, 0],
        textColor: '#ff00ff',
        textSize: 10
      }, {
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/conv40.png',
        height: 36,
        width: 40,
        anchor: [6, 0],
        textColor: '#ff0000',
        textSize: 11
      }, {
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/conv50.png',
        width: 50,
        height: 45,
        anchor: [8, 0],
        textSize: 12
      }],
      [{
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/heart30.png',
        height: 26,
        width: 30,
        anchor: [4, 0],
        textColor: '#ff00ff',
        textSize: 10
      }, {
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/heart40.png',
        height: 35,
        width: 40,
        anchor: [8, 0],
        textColor: '#ff0000',
        textSize: 11
      }, {
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/heart50.png',
        width: 50,
        height: 44,
        anchor: [12, 0],
        textSize: 12
      }],
      [{
        url: 'https://cdn.jsdelivr.net/npm/marker-clusterer-plus@2.1.4/images/pin.png',
        height: 48,
        width: 30,
        anchor: [-18, 0],
        textColor: '#ffffff',
        textSize: 10,
        iconAnchor: [15, 48]
      }]
    ];
    
    var markerClusterer = null;
    var map = null;
    var imageUrl = 'http://chart.apis.google.com/chart?cht=mm&chs=24x32&' +
      'chco=FFFFFF,008CFF,000000&ext=.png';
    
    function refreshMap(data) {
      if (markerClusterer) {
        markerClusterer.clearMarkers();
      }
    
      var markers = [];
    
      var markerImage = new google.maps.MarkerImage(imageUrl,
        new google.maps.Size(24, 32));
    
      for (var i = 0; i < data.photos.length; ++i) {
        var latLng = new google.maps.LatLng(data.photos[i].latitude,
          data.photos[i].longitude)
        var marker = new google.maps.Marker({
          position: latLng,
          draggable: true,
          icon: markerImage
        });
        markers.push(marker);
      }
    
      var zoom = parseInt(document.getElementById('zoom').value, 10);
      var size = parseInt(document.getElementById('size').value, 10);
      var style = parseInt(document.getElementById('style').value, 10);
      zoom = zoom === -1 ? null : zoom;
      size = size === -1 ? null : size;
      style = style === -1 ? null : style;
    
      markerClusterer = new MarkerClusterer(map, markers, {
        maxZoom: zoom,
        gridSize: size,
        styles: styles[style],
        imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
      });
    }
    
    function initialize() {
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 2,
        center: new google.maps.LatLng(39.91, 116.38),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
    
      var refresh = document.getElementById('refresh');
      google.maps.event.addDomListener(refresh, 'click', refreshMap);
    
      var clear = document.getElementById('clear');
      google.maps.event.addDomListener(clear, 'click', clearClusters);
    
      $.getJSON("https://api.myjson.com/bins/6ad78", function(data, textStatus, jqXHR) {
        console.log(textStatus);
        refreshMap(data);
      });
    }
    
    function clearClusters(e) {
      e.preventDefault();
      e.stopPropagation();
      markerClusterer.clearMarkers();
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    body {
      margin: 0;
      padding: 10px 20px 20px;
      font-family: Arial;
      font-size: 16px;
    }
    
    #map-container {
      padding: 6px;
      border-width: 1px;
      border-style: solid;
      border-color: #ccc #ccc #999 #ccc;
      -webkit-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px;
      -moz-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px;
      box-shadow: rgba(64, 64, 64, 0.1) 0 2px 5px;
      width: 800px;
    }
    
    #map {
      width: 800px;
      height: 400px;
    }
    
    #actions {
      list-style: none;
      padding: 0;
    }
    
    #inline-actions {
      padding-top: 10px;
    }
    
    .item {
      margin-left: 20px;
    }
    
    
    
    
    

    An example of MarkerClusterer v3

    Max zoom level: Cluster size: Cluster style: Clear

提交回复
热议问题