I have two different colored CircleMarkers for two geojson layer. Is it possible to add a letter into those circles, without using divIcon?
This is where I give them the color.
function getColor(iconcategory) {
return iconcategory == 'tram' ? 'black' :
iconcategory == 'ubahn' ? 'purple' :
'red';
}
This is the popup.
function forEachFeature(feature, layer) {
var popupContent = "<b>" +
feature.properties.name + "</b>" ;
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
};
This is the rest.
onEachFeature: forEachFeature,
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, {
radius: 10,
opacity: .9,
color: getColor(feature.properties.iconcategory),
fillColor: getColor(feature.properties.iconcategory),
fillOpacity: 0.3,
// html: feature.properties.iconcategory[0].toUpperCase(),
})
.bindTooltip(feature.properties.name);
The html tag from the divIcon isn't working. Is there any chance that I don't have to use the divIcon
And… is there any possibility to add a second circle to the circle or do I have to do this with a divIcon as well? I use the circlemarker, because it's easy and doesn't require a lot of code.
Best Answer
This answer is based on @IvanSanchez idea of using
L.divIcon
for text and draw circle around it via CSS style.The main factor to make it successful is to set icon width and height to twice the circle radius plus twice the circle border width, otherwise circle will become ellipse instead.
To make solution flexible, there is base CSS class
circle
for creating circle and then subclassescircle1
andcircle2
for individual styling (could be more of course). Desired marker is then created via function, where circle radius, circle border width and circle CSS subclass are input parameters.So, it this are the CSS classes for circle and text styling:
and this is marker creating function:
then with the following calls:
this is the result: