简体   繁体   中英

OpenLayers 4 zoom to cluster

Im using OpenLayers 4 and have a layer with an ol.source.Cluster as source. When I click on a clustered point I want to zoom in to the extent of the original points that makes up the cluster. My problem is that I can not find these original points anywhere.

I have tried to calculate an extent from the distance I use on the cluster but the result is not satisfying.

Any idea how to determine which the original points behind a cluster point are?

<html>
  <head>
    <title>Cluster</title>
    <link rel="stylesheet" href="https://openlayers.org /en/v4.1.1/css/ol.css" type="text/css">
    <script src="https://openlayers.org/en/v4.1.1/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script>

      var geojsonObject = {
        'type': 'FeatureCollection',
        'crs': {
          'type': 'name',
          'properties': {
            'name': 'EPSG:3857'
          }
        },
        'features': [{
          'type': 'Feature',
          'geometry': {
            'type': 'Point',
            'coordinates': [0, 0]
          }
        }, {
          'type': 'Feature',
          'geometry': {
            'type': 'Point',
            'coordinates': [9, 9]
          }
        },{
          'type': 'Feature',
          'geometry': {
            'type': 'Point',
            'coordinates': [10, 10]
          }
        }]
      };

      var source = new ol.source.Vector({
        features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
      });

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

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

      var select = new ol.interaction.Select({
        layers: [layer]     
      });

      var view = new ol.View({
        center: [5, 5],
        zoom: 20
      });

      var map = new ol.Map({
        target: 'map',
        layers: [new ol.layer.Tile({
          source: new ol.source.OSM()
          }),layer],
        view: view
      });

      map.addInteraction(select);

      var selectedFeatures = select.getFeatures();

      selectedFeatures.on('add', function(event) {
        // event.target only contains the clustered point
        var feature = event.target.item(0);
      });   

    </script>
  </body>
</html>

I found the answer myself in a OpenLayers example .

In the selected feature function you can do

var originalFeatures = feature.get('features');

To get the original features, and then in my case to zoom to the extent

var extent = new ol.extent.createEmpty();
originalFeatures.forEach(function(f, index, array){
    ol.extent.extend(extent, f.getGeometry().getExtent());
});
map.getView().fit(extent, map.getSize());

You can get the features in the cluster from:

source.getFeatures()

You can get the extent from:

source.getExtent()

Where source is an instance of ol.source.Cluster

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM