57

Is it possible to add text to a custom icon marker? I want to avoid having to edit the icon in an image editor just to add the text.

I've created my custom icon marker like so:

var airfieldIcon = L.icon({
        iconUrl: 'images/airfield.png',
        iconSize: [48,48]
});

var airfield = L.geoJson (airfield, {
    pointToLayer: function(feature,latlng){
        return L.marker(latlng, {
            icon: airfieldIcon
        })
    }
}).addTo(map);

How would I add the text "Banff Airfield" as a label to this icon? Is the easiest and most efficient way through using an image editor? if so, I will do that method, but wondering if there was a better way. Thanks!

redshift
  • 4,815
  • 13
  • 75
  • 138

4 Answers4

76

You could use a L.DivIcon:

Represents a lightweight icon for markers that uses a simple div element instead of an image.

http://leafletjs.com/reference.html#divicon

Put your image and text in it's HTML, sprinkle some CSS to make it appear the way you want and you're good to go

new L.Marker([57.666667, -2.64], {
    icon: new L.DivIcon({
        className: 'my-div-icon',
        html: '<img class="my-div-image" src="http://png-3.vector.me/files/images/4/0/402272/aiga_air_transportation_bg_thumb"/>'+
              '<span class="my-div-span">RAF Banff Airfield</span>'
    })
});

Another option is to use the Leaflet.Label plugin:

Leaflet.label is plugin for adding labels to markers & shapes on leaflet powered maps.

iH8
  • 27,722
  • 4
  • 67
  • 76
58

As of leaflet version 1.0.0, you can add a label without using a plugin (the plugin has been rolled into the core functionality).

Example:

var marker = L.marker(latlng)
    .bindTooltip("Test Label", 
    {
        permanent: true, 
        direction: 'right'
    }
);

This yields a marker on the map with a label of "Test Label" which is always displayed to the right of the marker's icon.

Mark
  • 4,970
  • 5
  • 42
  • 66
  • 2
    Great answer! So happy that Leaflet rolled this into the core package. API reference is here - http://leafletjs.com/reference-1.0.3.html#layer-bindtooltip – MattSidor Feb 07 '17 at 21:20
  • 1
    To make it look as google maps markers: `L.marker(latlng).bindTooltip("A",{permanent: true, direction: 'top',offset:L.point(-14, -5)}).addTo(map);` – Jawad Al Shaikh Sep 15 '21 at 08:48
4

To further explore Mark's answer, if you want to know an easy way to add text (number) over a marker like this:

enter image description here

You just have to proceed as follows:

1. Instantiate an icon (map.js)

var stepIcon = L.icon({
    iconUrl: 'graphic/yellow-circle.png', // the background image you want
    iconSize: [40, 40], // size of the icon
});

2.1 Setting the icon (map.js)

var marker = new L.marker([39.5, 77.3], { icon:stepIcon});
marker.bindTooltip("12" //specific number, {
                            permanent: true,
                            direction: 'center',
                            className: "my-labels"
                         });
marker.addTo(map);

2.2 Setting the icon (map.css)

.leaflet-tooltip.my-labels {
  background-color: transparent;
  border: transparent;
  box-shadow: none;
  font-weight: bold;
  font-size: 30px;
  }

Make sure you have imported your .css file into your .html page.

thomaslprr
  • 91
  • 5
0

this is out of box solution it may not suitable for everyone but it works for me simply you can add marker of icon then marker of text Like this :

    var MarkerIcon = L.icon(feature.geometry.properties.icon);
    var MarkerText = L.divIcon(
    {className: TextPositionClass,
     html:'<div>'+ displayText+'</div>',
     iconSize: null
    });
        
        
  let  marker = L.marker(latlng,  {icon: MarkerIcon}).addTo(this.map); // add marker 
  let label = L.marker(latlng, {icon: MarkerText}).addTo(this.map); // add text on marker
taha mosaad
  • 567
  • 5
  • 13