1

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.

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.