使Leaflet工具提示可点击

Str*_*nio 6 javascript click event-handling leaflet

我想在传单地图上添加工具提示(无标记)并使它们可单击。以下代码添加了一个工具提示,但它不可点击:

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!");
});
Run Code Online (Sandbox Code Playgroud)

我该如何运作?

nik*_*shr 7

要接收L.Tooltip对象的点击,您需要:

到目前为止的演示

var el = tooltip.getElement();
el.addEventListener('click', function() {
   console.log("click");
});
Run Code Online (Sandbox Code Playgroud)
var el = tooltip.getElement();
el.style.pointerEvents = 'auto';
Run Code Online (Sandbox Code Playgroud)
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';
Run Code Online (Sandbox Code Playgroud)

如果您想创建组件或直接监听工具提示对象,您可以扩展L.Tooltip以将这些更改直接烘焙到定义中:

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()));
});
Run Code Online (Sandbox Code Playgroud)

还有一个演示

html, body {
  height: 100%;
  margin: 0;
}
#map {
  width: 100%;
  height: 180px;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
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()));
});
Run Code Online (Sandbox Code Playgroud)


Nja*_*Nja 6

简单的解决方案:将interactive属性设置为 true:

交互式工具提示

在反应传单中

<Tooltip interactive={true}><Tooltip />
Run Code Online (Sandbox Code Playgroud)