Adding buttons inside Leaflet popup(在 Leaflet 弹出窗口中添加按钮)
问题描述
当我尝试在 Leaflet 弹出窗口中添加按钮时遇到问题.单击地图时会生成弹出窗口.
I got a problem when I try to add buttons inside a Leaflet popup. The popup is generated when you click on the map.
理想情况下,我想弹出 2 个按钮:
Ideally I want to popuo to show 2 buttons:
- 从这里开始
- 然后去这个位置
这个草图是我想要的结果的一个例子:
This sketch is an example of the result I want:
________________________________________________
|You clicked the map at LatLng(XXXXX,XXXXX) |
| --------------- ------------------- |
| |Start from here| |Go to this location| |
| --------------- ------------------- |
|___________________ ___________________________|
/
这是我在弹出窗口中看到的内容:您在 LatLng(XXXXX,XXXX) [object HTMLButtonElement] 处单击了地图
this is what I get inside my popUp : You clicked the map at LatLng(XXXXX,XXXX) [object HTMLButtonElement]
我正在尝试使用 L.domUtil 创建按钮
I am trying to create the buttons using L.domUtil
defineYourWaypointOnClick(e: any) {
var choicePopUp = L.popup();
var container = L.DomUtil.create('div'),
startBtn = this.createButton('Start from this location', container),
destBtn = this.createButton('Go to this location', container);
choicePopUp
.setLatLng(e.latlng)
.setContent('You clicked the map at ' + e.latlng.toString() + '<br>' + startBtn)
.openOn(this.map);
L.DomEvent.on(startBtn, 'click', () => {
alert("toto");
});
L.DomEvent.on(destBtn, 'click', () => {
alert("tata");
});
}
createButton(label: string, container: any) {
var btn = L.DomUtil.create('button', '', container);
btn.setAttribute('type', 'button');
btn.innerHTML = label;
return btn;
}
我从这里调用我的方法:
I call my method from here :
this.map.on('click', (e: any) => {
this.defineYourWaypointOnClick(e);
});
提前感谢您能给我的任何帮助:)
Thank you in advance for any help you can give me :)
推荐答案
您应该使用 innerHTML 向您的传单添加按钮,如下所示
You should be using innerHTML to add buttons to your leaflet as below
defineYourWaypointOnClick(e: any) {
var choicePopUp = L.popup();
var container = L.DomUtil.create('div');
//////////////////////////////////////////////////////////////////////////////////////////////
///////////modified here
startBtn = this.createButton('Start from this location', container),
destBtn = this.createButton('Go to this location', container);
div.innerHTML = ''+startBtn+ ' ' + destBtn ;
//////////////////////////////////////////////////////////////////////////////////////////////
choicePopUp
.setLatLng(e.latlng)
.setContent('You clicked the map at ' + e.latlng.toString() + '<br>' + startBtn)
.openOn(this.map);
L.DomEvent.on(startBtn, 'click', () => {
alert("toto");
});
L.DomEvent.on(destBtn, 'click', () => {
alert("tata");
});
}
createButton(label: string, container: any) {
var btn = L.DomUtil.create('button', '', container);
btn.setAttribute('type', 'button');
btn.innerHTML = label;
return btn;
}
这篇关于在 Leaflet 弹出窗口中添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Leaflet 弹出窗口中添加按钮
基础教程推荐
- 直接将值设置为滑块 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01