2017-07-07 8 views
0

私はOpenLayers 4を使用し、ソースとしてol.source.Clusterを持つレイヤーを持っています。クラスタ化されたポイントをクリックすると、クラスタを構成する元のポイントの範囲を拡大したいと考えています。私の問題は、私がこれらのオリジナルのポイントをどこにも見つけることができないことです。OpenLayers 4 zoom to cluster

私はクラスタで使用する距離からエクステントを計算しようとしましたが、結果は満足できません。

クラスタポイントの背後にある元のポイントはどのように判断するのですか?

<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

あなたがから、クラスタ内の機能を得ることができます:あなたがからの範囲を得ることができます

source.getFeatures()

source.getExtent()

sourceはol.source.Clusterのインスタンスです。

+0

をズームするしかし、それは私のすべての機能を与えるだろう

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

を行うことができ、選択した機能の機能で

ソース、私はちょうど1つのクラスター化されたポイント(私が選択したもの)を構成する機能を必要としています。私はとにかく答えを見つけ、将来の参考のために投稿します。 – user613068

0

私自身はOpenLayers exampleにいます。あなたがオリジナルの特徴を取得するには、その後、私の場合に限り

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