主な問題は、あなたが関数内で使う変数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思う:ここでは、コードですマーカーの追加と削除を行います。 –