주요 문제는 당신이 함수 내에서 사용하는 변수 map
addMarker
은 생성 된 맵을 저장하는 변수가 아닙니다. 이 문제를 해결하는 방법은 여러 가지가 있지만 가장 쉬운 방법은 생성 된 맵을 첫 번째 줄에 선언 된 변수 map
에 할당하는 것입니다.
var map, newMarker, markerLocation;
$(function(){
// Initialize the map
// This variable map is inside the scope of the jQuery function.
// var map = L.map('map').setView([38.487, -75.641], 8);
// Now map reference the global map declared in the first line
map = L.map('map').setView([38.487, -75.641], 8);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
maxZoom: 18
}).addTo(map);
newMarkerGroup = new L.LayerGroup();
map.on('click', addMarker);
});
function addMarker(e){
// Add marker to map at click location; add popup window
var newMarker = new L.marker(e.latlng).addTo(map);
}
내가 당신을 도움이 될 것입니다 http://stackoverflow.com/questions/9912145/leaflet-how-to-find-existing-markers-and-delete-markers/24342585#24342585 생각 : 여기에 코드입니다 마커를 추가하고 삭제할 수 있습니다. –