0

I'm using Leaflet js and I want a user to be able to add a single point to the map (and not get trigger happy and add lots of pins), what do I need to do to modify the below so that this can happen.

map.on('click', function(e){
   var Marker = L.marker(e.latlng,{
       draggable: true
   }).addTo(map);

Si

0

Here is an jQuery-based example where the user can click a button to enable adding a single marker. After adding a marker, the map click function is removed from the map - no additional markers can be added until the button is pressed again.

HTML

<button>Click to add a single marker</button>

JavsScript/jQuery

$(button).on('click', function() {
    enableAddMarker();
}

function enableAddMarker() {
    map.on('click', addMarker)
}

function addMarker(e) {
    var Marker = L.marker(e.latlng,{
         draggable: true
    })
    Marker.addTo(map);
    map.off('click', addMarker);

}
1

Maybe something like that:
Add a global variable to your code var count = 0;.
In your function add two things a line, count += 1.
In the function have an, if (count < 10){ draggable: true } else {draggable: false}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.