Making Leaflet tooltip clickable
Asked Answered
M

3

11

I want to add tooltips on a Leaflet map (without markers) and make them clickable. The following code adds a tooltip but it's not clickable:

var tooltip = L.tooltip({
        direction: 'center',
        permanent: true,
        interactive: true,
        noWrap: true,
        opacity: 0.9
    });
tooltip.setContent( "Example" );
tooltip.setLatLng(new L.LatLng(someLat, someLon));
tooltip.addTo(myLayer);
tooltip.on('click', function(event) {
    console.log("Click!");
});

How can I make it work?

Mentor answered 25/1, 2018 at 12:59 Comment(0)
S
7

To receive clicks on a L.Tooltip object, you'll need to :

  • set up a listener on the associated DOM object :

    var el = tooltip.getElement();
    el.addEventListener('click', function() {
       console.log("click");
    });
    
  • remove the pointer-events: none property set on that element:

    var el = tooltip.getElement();
    el.style.pointerEvents = 'auto';
    

A demo so far

var map = L.map(document.getElementById('map')).setView([48.8583736, 2.2922926], 4);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

var tooltip = L.tooltip({
    direction: 'center',
    permanent: true,
    interactive: true,
    noWrap: true,
    opacity: 0.9
});
tooltip.setContent( "Example" );
tooltip.setLatLng(new L.LatLng(48.8583736, 2.2922926));
tooltip.addTo(map);

var el = tooltip.getElement();
el.addEventListener('click', function() {
    console.log("click");
});
el.style.pointerEvents = 'auto';
html, body {
  height: 100%;
  margin: 0;
}
#map {
  width: 100%;
  height: 180px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
   
<div id='map'></div>

If you want to create a component or listen directly to a tooltip object, you can extend L.Tooltip to bake those alterations directly into the definition:

L.ClickableTooltip = L.Tooltip.extend({
    onAdd: function (map) {
        L.Tooltip.prototype.onAdd.call(this, map);

        var el = this.getElement(),
            self = this;

        el.addEventListener('click', function() {
            self.fire("click");
        });
        el.style.pointerEvents = 'auto';
    }
});

var tooltip = new L.ClickableTooltip({
    direction: 'center',
    permanent: true,
    noWrap: true,
    opacity: 0.9
});
tooltip.setContent( "Example" );
tooltip.setLatLng(new L.LatLng(48.8583736, 2.2922926));
tooltip.addTo(map);

tooltip.on('click', function(e) {
    console.log("clicked", JSON.stringify(e.target.getLatLng()));
});

And a demo

L.ClickableTooltip = L.Tooltip.extend({

    onAdd: function (map) {
        L.Tooltip.prototype.onAdd.call(this, map);

        var el = this.getElement(),
            self = this;

        el.addEventListener('click', function() {
            self.fire("click");
        });
        el.style.pointerEvents = 'auto';
    }
});


var map = L.map(document.getElementById('map')).setView([48.8583736, 2.2922926], 4);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

var tooltip = new L.ClickableTooltip({
    direction: 'center',
    permanent: true,
    noWrap: true,
    opacity: 0.9
});
tooltip.setContent( "Example" );
tooltip.setLatLng(new L.LatLng(48.8583736, 2.2922926));
tooltip.addTo(map);

tooltip.on('click', function(e) {
    console.log("clicked", JSON.stringify(e.target.getLatLng()));
});
html, body {
  height: 100%;
  margin: 0;
}
#map {
  width: 100%;
  height: 180px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/leaflet.js"></script>
   
<div id='map'></div>
Soil answered 26/1, 2018 at 11:9 Comment(3)
I added el.title = this.options.title; to allow titles functionality.Sen
How can you access the tooltip element inside the event listener? It only seems to be possible to get the html element but getting the actual tooltip object, for example to get its coordinates, seems to not be possible.Invasion
@Invasion In the ClickableTooltip component, it's available as target in the event object. I've modified my exampleSoil
M
7

Easy solution: set the interactive property to true:

tooltip interactive

in react-leaflet

<Tooltip interactive={true}><Tooltip />
Mandell answered 9/9, 2020 at 9:36 Comment(1)
Most simple solution for the question asked, did exactly what I was looking for.Souza
S
-1

new L.Tooltip().setLatLng([]).addTo(map).bindTooltip(your html, {permanent: true,interactive: true,direction: 'center',className: 'yourclassname'});

Saks answered 4/5, 2018 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.