Leaflet Marker Tooltip centered

0x45 picture 0x45 · Feb 14, 2018 · Viewed 9.9k times · Source

I want to display the tooltip centered (inside) of the marker.

Background

I want to display a count for each marker (first -> last) and I've found out I could do that with tooltip (Any better suggestions)

so my marker looks like this now,

var marker = L.marker(latlng, {icon: blueIcon}).bindTooltip(feature.properties.count, 
                    {
                permanent: true, 
                direction: 'right'
            });

I couldn't find any further documentation on direction centered or similar.

The tooltip provides the functionality to see which was the first and last marker however it seems not to be best practice.

So my questions:

  • Is there a better solution than tooltip?
  • how can I display the count centered in the marker?

Example

Current: enter image description here

wanted: enter image description here

(Here I would make the background invis so I could just see the text.)

Answer

0x45 picture 0x45 · Feb 14, 2018

I've found out you could add direction: center. Ref: http://leafletjs.com/reference-1.0.0.html#tooltip-direction

I've solved my problem with DivIcon

var numberIcon = L.divIcon({
                    className: "number-icon-default",
                    iconSize: [25, 41],
                    iconAnchor: [10, 44],
                    popupAnchor: [3, -40],
                    html: feature.properties.count        
              });

with the css like

    .number-icon-default
{
    background-image: url("#{request.contextPath}/lib/leaflet/images/marker-icon.png");
    text-align:center;
    color:Black;   
    text-shadow: 1px 1px #000000;
    font-size: large;
    font-weight: bold;
}

Result

enter image description here