問題描述
我有一個 leaflet 地圖,我想為我的多邊形添加一個 html 標題(工具提示).如果我使用純 JQuery:
I have a leaflet map and I would like to add a html title (tooltip) to my polygon. If I use plain JQuery:
$('<title>my tooltip</title>').appendTo()
標題被添加到 DOM 但不可見.請參閱此處了解更多詳細信息,但如果我遵循該解決方案,我可以不再使用傳單功能.
The title gets added to the DOM but is not visible. See here for more details but if I follow that solution, I can no longer use the leaflet features.
我也嘗試了 leaflet.label 插件,但標簽會隨著鼠標指針移動.我只想要在懸停后不久出現在一個位置的標準瀏覽器標題工具提示)
I also tried the leaflet.label plugin but the label moves around with the mouse pointer. I just want the standard browser title tooltip that appears in one position shortly after on hover)
感謝您的幫助
推薦答案
Leaflet 1.0.0 有一個新的內置 L.tooltip
類,棄用 Leaflet.label 插件.工具提示指向形狀中心,不隨鼠標移動.
Leaflet 1.0.0 has a new built-in L.tooltip
class that deprecates the Leaflet.label plugin. The tooltip points at the shape center and does not move with the mouse.
L.polygon(coords).bindTooltip("my tooltip").addTo(map);
演示:https://jsfiddle.net/3v7hd2vx/91/
要解決 OP 關于在多邊形中心顯示工具提示的評論,當多邊形很大并且當前縮放很高時可能會看不見,您可以使用 sticky
選項:
To address OP's comment about tooltip being displayed at the polygon center, which can be out of view when the polygon is very big and current zoom is high, you can use the sticky
option:
L.polygon(coords).bindTooltip("my tooltip", {
sticky: true // If true, the tooltip will follow the mouse instead of being fixed at the feature center.
}).addTo(map);
更新的演示:https://jsfiddle.net/3v7hd2vx/402/
這篇關于如何將 html 標題(工具提示)添加到 leaflet.js 多邊形?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!