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

时间:2019-05-23 12:10:26

标签: openlayers openlayers-5

我正在使用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
 });

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

1 个答案:

答案 0 :(得分:1)

如果您要在单击时放大群集,则可以获取群集内要素的范围并使地图适合该范围。这是一个示例(使用添加到示例https://openlayers.org/en/v4.6.5/examples/cluster.html中的zoomCluster函数)

  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>