在Leaflet弹出窗口中添加按钮

Une*_*ess 6 javascript leaflet typescript angular

当我尝试在Leaflet弹出窗口中添加按钮时出现问题.单击地图时会生成弹出窗口.

理想情况下,我想popo显示2个按钮:

  • 从这里开始
  • 并前往这个位置

这个草图是我想要的结果的一个例子:

 ________________________________________________
|You clicked the map at LatLng(XXXXX,XXXXX)      |
|  ---------------    -------------------        |
| |Start from here|  |Go to this location|       |
|  ---------------    -------------------        |
|___________________  ___________________________|
                   \/
Run Code Online (Sandbox Code Playgroud)

这就是我在popUp中的内容:你在LatLng点击了地图(XXXXX,XXXX)[object HTMLButtonElement]

我正在尝试使用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;
}
Run Code Online (Sandbox Code Playgroud)

我从这里打电话给我的方法:

this.map.on('click', (e: any) => {
  this.defineYourWaypointOnClick(e);
});
Run Code Online (Sandbox Code Playgroud)

提前感谢您的任何帮助,你可以给我:)

Ara*_*ind 4

您应该使用innerHTML 将按钮添加到您的传单中,如下所示

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

  • `div` 未定义。`startBtn` 和 `destBtn` 仍然是 HTML 元素,而不是字符串。 (2认同)