calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
MIT License
735 stars 230 forks source link

remove geojson/clear shp layer #86

Closed alfathdirk closed 7 years ago

alfathdirk commented 7 years ago
let state = 1
let map = new L.map('rendermaps', {
        center: new L.LatLng(-2.0836809794977484, 113.63967449468988), 
        zoom: 5, 
        layers: [googleStreets]
    });

let geo = L.geoJson({features:[]},{onEachFeature:function popUp(f,l){
        let out = [];
        if (f.properties){
            for(let key in f.properties){
                out.push(key+": "+f.properties[key]);
            }
            l.bindPopup(out.join("<br />"));
        }
    }});
    let base = '../shp/testfile.zip';
    if(state === 1) {
        geo.addTo(map);
        shp(base).then(function(data){
            geo.addData(data);
        });
    } else {
        map.removeLayer(geo);
    }

i use removeLayer() and clearLayers() but no effect on map..

how to remove geojson when the state change to 0 ?

calvinmetcalf commented 7 years ago

um this is probably more of an issue with leaflet, but if the state changes that code won't run, it only runs once, you may need to add a function that you can call that checks the state