How do you add marker to map using leaflet map.on('click', function) event handler

后端 未结 3 671
抹茶落季
抹茶落季 2020-12-10 01:55

I\'m trying to use an event handler to add a marker to the map. I can manage this with a callback function, but not when I separate the function from the event handler.

3条回答
  •  眼角桃花
    2020-12-10 02:04

    in your fiddle code, your function is in the wrong scope. try moving the function inside the map function instead of in it's own scope... i.e. instead of:

    });
    
    function addMarker(e){
    // Add marker to map at click location; add popup window
    var newMarker = new L.marker(e.latlng).addTo(map);
    }
    

    use

    function addMarker(e){
    // Add marker to map at click location; add popup window
    var newMarker = new L.marker(e.latlng).addTo(map);
    }
    });
    

提交回复
热议问题