Suppose we have a project using leaflet.js. When clicking on the map i want a popup to appear that gives me a dropdown menu with a submit button. The dropdown function has 3 options a,b,c. I want to be able to create a submit button that creates a marker at the location (LatLong) of the popup. this marker must show the string of the choice made in the dropdown menu.
First of all i tried option 1 in the code, however a i get a reference error whenever i create a handler. I also tried the option 2 where one creates a div object. I tried creating the div object manually as well with ID "popupcontent" which worked, however the handler does not remember the lat-long cooridnates.
In summary:
I want to create a button that makes a marker at the place clicked with the content of either a,b,c depending on the choice in the dropdown menu.
option 1
var dropdown =
`Type to submit:
<select>
<option value="a">a</option>
<option value="b">b</option>
<option value = "c">c</option>
</select>: <button onclick="buttonFunction()">submit</button>`
var popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent(dropdown)
.openOn(mymap);
//L.marker(e.latlng).addTo(mymap);
//L.marker([e.latlng.lat, e.latlng.long]).addTo(mymap)
}
option2
var popup = L.popup();
var container = L.DomUtil.create('div');
var btn = L.DomUtil.create('button', '', container);
btn.setAttribute('type', 'button');
btn.innerHTML = 'hello kees';
L.DomEvent.on(btn, 'click', () => {
alert("hello kees");
});
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent(container)
.openOn(mymap);
//L.marker(e.latlng).addTo(mymap);
//L.marker([e.latlng.lat, e.latlng.long]).addTo(mymap)
}
GOAL:
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent( "LatLong: "+ e.latlng.lat + "," + e.latlng.lng)
.openOn(mymap);
lat = e.latlng.lat
long = e.latlng.lng
}
function buttonFunction() {
var selected = document.getElementById("popupContent");
var value = dropDown.options[dropDown.selectedIndex].text;
L.marker(lat,long).addTo(mymap);
}
buttonFunction
? Add another marker? I didn't understand. Can you rephrase your description to be more clear? – kboulbuttonFunction
and create a marker in the position of the popup therefore under the popup. correct? – kboul