admin管理员组文章数量:1316847
I got a problem when I try to add buttons inside a Leaflet popup. The popup is generated when you click on the map.
Ideally I want to popuo to show 2 buttons:
- start from Here
- and go to this location
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| |
| --------------- ------------------- |
|___________________ ___________________________|
\/
this is what I get inside my popUp : You clicked the map at LatLng(XXXXX,XXXX) [object HTMLButtonElement]
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 :)
I got a problem when I try to add buttons inside a Leaflet popup. The popup is generated when you click on the map.
Ideally I want to popuo to show 2 buttons:
- start from Here
- and go to this location
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| |
| --------------- ------------------- |
|___________________ ___________________________|
\/
this is what I get inside my popUp : You clicked the map at LatLng(XXXXX,XXXX) [object HTMLButtonElement]
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 :)
Share Improve this question edited Mar 4, 2017 at 18:14 BenM 53.2k26 gold badges115 silver badges172 bronze badges asked Mar 4, 2017 at 18:06 UnessUness 1731 gold badge1 silver badge9 bronze badges1 Answer
Reset to default 4You 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;
}
本文标签: javascriptAdding buttons inside Leaflet popupStack Overflow
版权声明:本文标题:javascript - Adding buttons inside Leaflet popup - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971669a2407877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论