I want to create a map with leaflet and give the user the opportunity to add a marker to that map from the user interface. The user shuld mark a point(market to th map). Then when the point is set I want to get the location (coordinates) of that marker and perform other operations. It should allow only one marker
How to add ''draw a marker" Control to leaflet Map and capture event?
1.2k Views Asked by Ernesto Ruiz At
2
There are 2 best solutions below
3
On
The Leaflet quick start guide has a section about dealing with events https://leafletjs.com/examples/quick-start/ There you can see an example of adding a popup when clicking on the map:
var popup = L.popup();
function onMapClick(e) {
popup
.setLatLng(e.latlng)
.setContent("You clicked the map at " + e.latlng.toString())
.openOn(mymap);
}
mymap.on('click', onMapClick);
We need to modify this function to add a marker instead:
var marker = L.marker();
function onMapClick(e) {
marker
.setLatLng(e.latlng)
.addTo(mymap);
}
mymap.on('click', onMapClick);

For doing this I had to define a LeafletWidget for the location field (Point) and adding some javascript to the form using the mesa class
worker_form.js