I have circle markers on my leaflet map using the below code which all works fine.
However I want to show the markers in a different color based on the attribute field named 'stype'.
Any help or guidance on how I can achieve this?
function siteslabels (feature, layer){
layer.bindPopup("<p class='info header'>"+
"<b>" + feature.properties.SITE + "</b>" +
"</br>" + feature.properties.Address1 +
"</br>" + feature.properties.stype +
"</p>");
};
var geojsonMarkerOptions = {
radius: 8,
fillColor: 'green',
color: 'black',
weight: 1,
opacity: 1,
fillOpacity: 0.8
};
L.geoJson(sites, {
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions);
},
onEachFeature: siteslabels
}).addTo(map);
I have circle markers on my leaflet map using the below code which all works fine.
However I want to show the markers in a different color based on the attribute field named 'stype'.
Any help or guidance on how I can achieve this?
function siteslabels (feature, layer){
layer.bindPopup("<p class='info header'>"+
"<b>" + feature.properties.SITE + "</b>" +
"</br>" + feature.properties.Address1 +
"</br>" + feature.properties.stype +
"</p>");
};
var geojsonMarkerOptions = {
radius: 8,
fillColor: 'green',
color: 'black',
weight: 1,
opacity: 1,
fillOpacity: 0.8
};
L.geoJson(sites, {
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions);
},
onEachFeature: siteslabels
}).addTo(map);
Share
edited May 30, 2017 at 11:20
Sukh_Internazionale
asked May 26, 2017 at 16:16
Sukh_InternazionaleSukh_Internazionale
811 gold badge1 silver badge5 bronze badges
2 Answers
Reset to default 5Got it sorted. Here is my code
<script>
<!-- long and lat for UK & Zoom level for whole of UK -->
var map = L.map('map',{ center:[54.038486, -1.948915], zoom: 5});
L.tileLayer('http://{s}.tile.osm/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
<!-- LAYERS/SITES -->
<!-- LAYERS/SITES POP UP CONTENT-->
function siteslabels (feature, layer){
layer.bindPopup("<p class='info header'>"+
"<b>" + feature.properties.SITE + "</b>" +
"</br>" + feature.properties.Address1 +
"</br>" + feature.properties.stype +
"</p>");
};
<!-- LAYERS/SITES POP UP COLOUR CIRCLE MARKERS->
function getColor(stype) {
switch (stype) {
case 'POP':
return 'orange';
case 'Regen':
return 'green';
case 'LLU':
return 'blue';
case 'Colo':
return 'purple';
case 'DMSU':
return 'blue';
default:
return 'white';
}
}
<!-- LAYERS/SITES ADD LAYER->
L.geoJson(sites, {
pointToLayer: function (feature, latlng) {
return new L.CircleMarker(latlng, {radius: 8,
fillOpacity: 1,
color: 'black',
fillColor: getColor(feature.properties.stype),
weight: 1,});
},
onEachFeature: siteslabels
}).addTo(map);
</script>
Check the Leaflet GeoJSON tutorial again, pointToLayer
option section.
If you render your points as CircleMarkers, you can easily set their style
to use different colours.
If you stick with Markers, you have to provide custom Icons. You can look for marker plugins, e.g. Leaflet.Extra-Markers.