2013-04-22 22 views
12

Desidero popolare un livello GeoJson con i dati e quindi filtrare dinamicamente le funzioni da mostrare.Leaflet: aggiorna il filtro GeoJson?

Ho ottenuto il filtro funzione per funzionare ma non so come cambiare il filtro e quindi aggiornare il livello.

C'è un modo per aggiornare il filtro dopo aver aggiunto i dati?

risposta

13

Ho fatto questo aggiungendo ogni tipo di funzionalità a un diverso LayerGroup in base a una proprietà della funzione. per esempio.

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] 
    } 
    } 
]; 

creare il livello 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);  
} 

Quindi è possibile richiamare le funzioni opuscolo map.addLayer/removeLayer esempio

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

Nel metodo GeoJSONaddData, il primo controllo è se i dati sono una raccolta di funzionalità, nel qual caso il metodo viene chiamato per ciascuna funzione.

Poi il filtro viene applicato come segue:

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

Quindi, se il filtro filtra i dati quando lo si aggiunge, che non vengono memorizzati o ricordato ovunque. La modifica del filtro non farà riapparire improvvisamente i dati.

È possibile mantenere un riferimento a geojson e reinizializzare il livello quando si modifica il filtro.

+0

Grazie, reinizializza livello medio addLayer/removeLayer? – dani

+0

Sospetto che si possa chiamare anche 'initialize (newgeojson, options)', ma non l'ho provato. Rimuovere e aggiungere funzionerà sicuramente. – flup

+0

In realtà 'map.removeLayer (gj)' quindi 'map.addLayer (gj)' non funziona. –

0

Vedere l'esempio sotto, si dispone di una mappa e un livello MyBus.

map.removeLayer(myBus); 

...add your data edit something ... 

myBus.addTo(map);