0
votes

Im using leaflet and am looking into GeoJSON, id like to add remove a polygon on a click event.

using the sample data from the GeoJSON examples in leaflet ive built the below, but I have no idea how to do this.

I did use poly layers without geojson and did manage to get a on click to add a layer but not removed, it just repeatedly added layers

thanks for any help

<html>
    <head>
            <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
            integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
            crossorigin=""/>
    </head>
<body>
    <a href="javascript:add_layer('Republican')">Republican</a> | <a href="javascript:add_layer('Democrat')">Democrat</a>
 <div id="map" style="height:700px;"></div>
 <script src="https://unpkg.com/[email protected]/dist/leaflet.js"
   integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
   crossorigin=""></script>
<script type="text/javascript">
var map = L.map('map').setView([40, -97.], 5);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
    attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
    maxZoom: 18,
    id: 'mapbox/streets-v11',
    accessToken: 'x'
}).addTo(map);

var states = [{
    "type": "Feature",
    "properties": {"party": "Republican"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-104.05, 48.99],
            [-97.22,  48.98],
            [-96.58,  45.94],
            [-104.03, 45.94],
            [-104.05, 48.99]
        ]]
    }
}, {
    "type": "Feature",
    "properties": {"party": "Democrat"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-109.05, 41.00],
            [-102.06, 40.99],
            [-102.03, 36.99],
            [-109.04, 36.99],
            [-109.05, 41.00]
        ]]
    }
}];

L.geoJSON(states);

function add_layer(value){
    if map.contains.feature.properties.party.case(value) {
        map.layer.remove(feature.properties.party.case(value))
    } else {
        feature.properties.party.case(value.addTo(map))
    }
}

</script>
</body>
</html>

EDIT: new function to add/remove, which returns "TypeError: Attempted to assign to readonly property." for the map.hasLayer line

function add_layer(value){
    if(map.hasLayer(value)) {
        map.removeLayer(value);
    }else{
        map.addLayer(value);
    }
}
1

1 Answers

1
votes

For your your question:

You can add and remove layers from map or layergroup with map.addLayer(layer) or layer.addTo(map) / map.removeLayer(layer) or layer.removeFrom(map).

But I think the best for you is to use the Layer Control https://leafletjs.com/examples/layers-control/


var states = [{
    "type": "Feature",
    "properties": {"party": "Republican"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-104.05, 48.99],
            [-97.22,  48.98],
            [-96.58,  45.94],
            [-104.03, 45.94],
            [-104.05, 48.99]
        ]]
    }
}, {
    "type": "Feature",
    "properties": {"party": "Democrat"},
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-109.05, 41.00],
            [-102.06, 40.99],
            [-102.03, 36.99],
            [-109.04, 36.99],
            [-109.05, 41.00]
        ]]
    }
}];


var democrat = L.geoJSON(states, {
        filter: function(feature, layer) {
        return feature.properties.party === "Democrat";
    }
});

var republican = L.geoJSON(states, {
        filter: function(feature, layer) {
        return feature.properties.party === "Republican";
    }
});

var overlayLayers= {
  "Republican": republican,
  "Democrat": democrat
};

L.control.layers(null,overlayLayers).addTo(mymap);

if you want to use external control (like your example):

function add_layer(value){
        map.removeLayer(republican);
        map.removeLayer(democrat);

    if(value === "Republican"){
        map.addLayer(republican);
    }else{
        map.addLayer(democrat);
    }
}

Also you can check if the map has a layer with: map.hasLayer(layer)