2013-04-22 26 views
12

私はGeoJsonレイヤーにデータを設定し、表示する機能を動的にフィルタリングしたいと考えています。リーフレット:GeoJsonフィルターを更新しますか?

私はフィルタ機能を働かせましたが、フィルタを変更してレイヤをリフレッシュする方法がわかりません。

データを追加した後にフィルタを更新する方法はありますか?

答えて

13

これは、フィーチャのプロパティに基づいて異なるフィーチャタイプを別のLayerGroupに追加することで行いました。例えば

にGeoJSON

var data =[ 
    { 
    type: "Feature", 
    properties: { 
     type: "type1" 
    }, 
    geometry: { 
     type: "Point", 
     coordinates: [-1.252,52.107] 
    } 
    }, 
    { 
    type: "Feature", 
    properties: { 
     type: "type2" 
    }, 
    geometry: { 
     type: "Point", 
     coordinates: [-2.252,54.107] 
    } 
    } 
]; 

GeoJSONレイヤ

//array to store layers for each feature type 
var mapLayerGroups = []; 

//draw GEOJSON - don't add the GEOJSON layer to the map here 
L.geoJson(data, {onEachFeature: onEachFeature})//.addTo(map); 

/* 
*for all features create a layerGroup for each feature type and add the feature to the layerGroup 
*/ 
function onEachFeature(feature, featureLayer) { 

    //does layerGroup already exist? if not create it and add to map 
    var lg = mapLayerGroups[feature.properties.type]; 

    if (lg === undefined) { 
     lg = new L.layerGroup(); 
     //add the layer to the map 
     lg.addTo(map); 
     //store layer 
     mapLayerGroups[feature.properties.type] = lg; 
    } 

    //add the feature to the layer 
    lg.addLayer(featureLayer);  
} 

を作成します。次に、例えばリーフレットmap.addLayer/removeLayer関数を呼び出すことができます

//Show layerGroup with feature of "type1" 
showLayer("type1"); 

/* 
* show/hide layerGroup 
*/ 
function showLayer(id) { 
    var lg = mapLayerGroups[id]; 
    map.addLayer(lg); 
} 
function hideLayer(id) { 
    var lg = mapLayerGroups[id]; 
    map.removeLayer(lg); 
} 
1

GeoJSONaddDataメソッドでは、データがフィーチャのコレクションであるかどうかが最初にチェックされます。この場合、メソッドはフィーチャごとに呼び出されます。次のように

その後、フィルタが適用されます。

var options = this.options; 
if (options.filter && !options.filter(geojson)) { return; } 

は、フィルタは、あなたがそれを追加し、データをフィルタリングするのであれば、それは保存されませんまたは任意の場所を覚えていました。フィルタを変更しても、データが突然表示されることはありません。

geojsonへの参照を保持し、フィルタを変更するときにレイヤーを再初期化することができます。

+0

ありがとう、レイヤーを再初期化しますか?addLayer/removeLayer? – dani

+0

私は 'initialize(newgeojson、options)'も呼び出すことができると思いますが、試してみませんでした。削除と追加は確実に機能します。 – flup

+0

実際に 'map.removeLayer(gj)'そして 'map.addLayer(gj)'は動作しません。 –

0

以下の例では、マップとmyBusレイヤがあります。

map.removeLayer(myBus); 

...add your data edit something ... 

myBus.addTo(map); 
関連する問題