2017-07-07 101 views
0

我使用OpenLayers 4並有一個ol.source.Cluster作爲源的圖層。當我點擊一個聚集點時,我想放大構成聚類的原始點的範圍。我的問題是,我無法在任何地方找到這些原始點。OpenLayers 4縮放到集羣

我試圖從我在集羣上使用的距離計算範圍,但結果並不令人滿意。

任何想法如何確定哪些原始點後面的聚點是?

source.getFeatures()

你可以從程度:

source.getExtent()

<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> 

回答

0

你可以從集羣中的功能哪裏來源是ol.source.Cluster的一個實例

+0

但是,這會給我的所有功能來源,我只想組成一個聚集點(我剛剛選擇的那個)的功能。無論如何,我找到了答案,並將其發佈供將來參考。 – user613068

0

我找到了答案我自己在OpenLayers example

在所選擇的特徵函數,你可以做

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

要獲得原始的功能,然後在我的情況下放大的程度

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());