[GIS] Hide/Show leaflet markers based on specific values

leafletpython

I have a Python list of dictionaries with a number of IPs and "Scores" for each IP.

I'm adding them to my front end map / leaflet using jinja2 like so:

var map = L.map('map')

    .addLayer(mapboxTiles)
    .setView([42.3610, -71.0587], 4);

    var data = {
      "type": "FeatureCollection",
      "features": [
        {% for obj in data %}
        {
          "type": "Feature",
          "properties": {
              {% for obj2 in obj.interfaces %}
                {% for obj3 in obj2.ip_addresses %}
            "full_name": "ID: {{ obj3.value }}",
                {% endfor %}
              {% endfor %}

            "Score": "Score: {{ obj.Score }}"

          },
          "geometry": {
            "type": "Point",
            "coordinates": [{{ obj.latitude }}, {{ obj.longitude }}]
          }

        }{% if not loop.last %},{% endif %}
        {% endfor %}

How can I set and remove different coloured markers for certain scores this way?

I know about using jQuery to remove certain classes, but because I'm using jinja, I'm finding it difficult to understand how I would go about this.

Best Answer

I think you're using the wrong tool (GeoJSON) for the job (showing markers depending on a condition). I'd approach the problem with individual markers, something like:

var map = L.map('map')

    .addLayer(mapboxTiles)
    .setView([42.3610, -71.0587], 4);


{% for obj in data %}

    marker = L.marker([{{ obj.latitude }}, {{ obj.longitude }}] , markerOptions);

    if ( obj.somethingsometing == 'visible' ) {
        marker.addTo(map);
    }

{% endfor %}

If you want to dinamically show/hide categories of markers, create a L.LayerGroup per category, add the markers to a group instead of to the map, and use a L.Control.Layers to let the user control the visibility.