当点击较小的集群时继续放大

Continue zooming in when smaller clusters are tapped

我正在使用 openlayers 5.3。我想在点击或单击较小的集群时继续放大。我已经使用了一些代码来实现这样的要求,但是它不起作用。

getStyleForCluster = (size: number): ol.style.Style => {
    let clusterStyle = (<any>window).styleCache[size];
    if (!clusterStyle) {

        clusterStyle = new ol.style.Style({
            image: new ol.style.Circle({
                radius: (Math.log(size) / Math.log(10)) * 3 + 10,
                fill: new ol.style.Fill({
                    color: this.getFillColorForPlace(size)
                })
            }),
            text: new ol.style.Text({
                text: size.toString(),
                fill: new ol.style.Fill({
                    color: "#fff"
                })
            })
        });

        (<any>window).styleCache[size] = clusterStyle;
    }
    return clusterStyle;
}


this.ClusterSource = new ol.source.Cluster({
      distance: distance,
      source: vectorSource
});

 var vectorLayer = new ol.layer.Vector({
       renderMode: 'image',
       source: this.ClusterSource,
       style: this.styleFunction,
       zIndex: 9999
 });

能否请您建议我如何解决此类问题。

如果您的意思是在单击集群时放大它,您可以获得集群内要素的范围并将地图与其相匹配。这是一个这样做的例子(使用添加到这个例子 zoomCluster 的函数 https://openlayers.org/en/v4.6.5/examples/cluster.html

  var distance = document.getElementById('distance');

  var count = 20000;
  var features = new Array(count);
  var e = 4500000;
  for (var i = 0; i < count; ++i) {
    var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
    features[i] = new ol.Feature(new ol.geom.Point(coordinates));
  }

  var source = new ol.source.Vector({
    features: features
  });

  var clusterSource = new ol.source.Cluster({
    distance: parseInt(distance.value, 10),
    source: source
  });

  var styleCache = {};
  var clusters = new ol.layer.Vector({
    source: clusterSource,
    style: function(feature) {
      var size = feature.get('features').length;
      var style = styleCache[size];
      if (!style) {
        style = new ol.style.Style({
          image: new ol.style.Circle({
            radius: 10,
            stroke: new ol.style.Stroke({
              color: '#fff'
            }),
            fill: new ol.style.Fill({
              color: '#3399CC'
            })
          }),
          text: new ol.style.Text({
            text: size.toString(),
            fill: new ol.style.Fill({
              color: '#fff'
            })
          })
        });
        styleCache[size] = style;
      }
      return style;
    }
  });

  var raster = new ol.layer.Tile({
    source: new ol.source.OSM()
  });

  var map = new ol.Map({
    layers: [raster, clusters],
    target: 'map',
    view: new ol.View({
      center: [0, 0],
      zoom: 2
    })
  });

  distance.addEventListener('input', function() {
    clusterSource.setDistance(parseInt(distance.value, 10));
  });

  var zoomCluster = function(pixel) {

    var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
      return feature;
    });

    if (feature) {

      var features = feature.get('features');
      if (features.length > 1) {

        var extent = ol.extent.createEmpty();
        features.forEach(function(feature) {
          ol.extent.extend(extent, feature.getGeometry().getExtent());
        });
        map.getView().fit(extent);

      }

    }

  };

  map.on('click', function(evt) {
    zoomCluster(evt.pixel);
  });
.map (
  width: 100%;
  height: 80%;
}
<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<div id="map" class="map"></div>
<form>
  <label>cluster distance</label>
  <input id="distance" type="range" min="0" max="100" step="1" value="40"/>
</form>

如果无法显示单个图钉,此版本将取消缩放:

  var distance = document.getElementById('distance');

  var count = 20000;
  var features = new Array(count);
  var e = 4500000;
  for (var i = 0; i < count; ++i) {
    var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
    features[i] = new ol.Feature(new ol.geom.Point(coordinates));
  }

  var source = new ol.source.Vector({
    features: features
  });

  var clusterSource = new ol.source.Cluster({
    distance: parseInt(distance.value, 10),
    source: source
  });

  var styleCache = {};
  var clusters = new ol.layer.Vector({
    source: clusterSource,
    style: function(feature) {
      var size = feature.get('features').length;
      var style = styleCache[size];
      if (!style) {
        style = new ol.style.Style({
          image: new ol.style.Circle({
            radius: 10,
            stroke: new ol.style.Stroke({
              color: '#fff'
            }),
            fill: new ol.style.Fill({
              color: '#3399CC'
            })
          }),
          text: new ol.style.Text({
            text: size.toString(),
            fill: new ol.style.Fill({
              color: '#fff'
            })
          })
        });
        styleCache[size] = style;
      }
      return style;
    }
  });

  var raster = new ol.layer.Tile({
    source: new ol.source.OSM()
  });

  var map = new ol.Map({
    layers: [raster, clusters],
    target: 'map',
    view: new ol.View({
      center: [0, 0],
      zoom: 2
    })
  });

  distance.addEventListener('input', function() {
    clusterSource.setDistance(parseInt(distance.value, 10));
  });

  var zoomCluster = function(pixel) {

    var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
      return feature;
    });

    if (feature) {

      var features = feature.get('features');
      if (features.length > 1) {

        var oldExtent = map.getView().calculateExtent();
        var extent = ol.extent.createEmpty();
        features.forEach(function(feature) {
          ol.extent.extend(extent, feature.getGeometry().getExtent());
        });
        map.getView().fit(extent, {
          callback: function() {
            var feature = clusterSource.forEachFeatureInExtent(extent, function(feature) {
              if (feature.get('features').length > 1) {
                return feature;
              }
            });
            if (feature) {
              map.getView().fit(oldExtent);
            }
          }
        });

      }

    }

  };

  map.on('click', function(evt) {
    zoomCluster(evt.pixel);
  });
.map (
  width: 100%;
  height: 80%;
}
<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<div id="map" class="map"></div>
<form>
  <label>cluster distance</label>
  <input id="distance" type="range" min="0" max="100" step="1" value="40"/>
</form>