2

I'm new to Leaflet and I want to work with attribute based marker icons. I followed a couple of tutorials and can display markers on my screen using geometry data from a PostGIS database.

var mapDataLayer = L.geoJson(geojson, {
    pointToLayer: function (feature, latlng) {
        var markerStyle = { 
            fillColor: "#F00",
            color: "#FFF",
            fillOpacity: 0.5,
            opacity: 0.8,
            weight: 1,
            radius: 8
        };

        return L.circleMarker(latlng, markerStyle);
    },
    onEachFeature: function (feature, layer) {
        var html = "";
        for (prop in feature.properties){
            html += prop+": "+feature.properties[prop]+"<br>";
        };
        layer.bindPopup(html);
    }
}).addTo(map);

Using this, I get a couple of points with the same color. Now I want to split up these points using different colors/icons for different attributes.

2 Answers 2

2

I've taken a look at feature.properties[prop]

I created a function to give the attributes "status" one out of five colors:

function getColor(status) {
return  status == "proposed"     ? '#800888' :
        status == "permitted"    ? '#969696' :
        status == "operational"  ? '#081d58' :
        status == "development"  ? '#006837' :
        status == "denied"       ? '#fed976' :
                                   '#252525';
}

Then i edited the code above a bit to include the getColor function.

    var mapDataLayer = L.geoJson(geojson, {
    pointToLayer: function (feature, latlng) {
        var markerStyle = { 
            fillColor: getColor(feature.properties.status),
            color: "#FFF",
            fillOpacity: 0.5,
            opacity: 0.8,
            weight: 1,
            radius: 8
        };

        return L.circleMarker(latlng, markerStyle);
    },
    onEachFeature: function (feature, layer) {
        var html = "";
        for (prop in feature.properties){
            html += prop+": "+feature.properties[prop]+"<br>";
        };
        layer.bindPopup(html);
    }
}).addTo(map);
2
  • 1
    Comparison operators
    – ghybs
    Commented Mar 8, 2016 at 12:34
  • That's it, forgot about the multiple = operator, thanks a lot ghybs
    – Jordi Z
    Commented Mar 8, 2016 at 12:48
1

You would probably simply need to follow an extra tutorial for custom markers: Markers With Custom Icons

In this tutorial, you’ll learn how to easily define your own icons for use by the markers you put on the map.

3
  • Thanks for your reply ghybs, i've seen that tutorial but it doesn't say anything about retrieving attributes from the database.
    – Jordi Z
    Commented Mar 7, 2016 at 14:06
  • By "attributes", what do you mean then? Are not they accessible through feature.properties[prop]?
    – ghybs
    Commented Mar 7, 2016 at 14:21
  • By attributes i mean attributes from the databases, in this case "status" or "county".
    – Jordi Z
    Commented Mar 8, 2016 at 12:26

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