[英]how to add marker on click event in leaflet.js
i want to add marker, when i click on map. but i dont know how to do it:(我想添加标记,当我点击 map 时。但我不知道该怎么做:(
by default I do not want to have a marker map默认情况下我不想有一个标记 map
i just wrote this code:我刚刚写了这段代码:
var mapOptions = {
center: [17.385044, 78.486671],
zoom: 10
}
var layer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
var map = new L.map('mapid', mapOptions);
map.addLayer(layer);
var markerOptions = {
title: "MyLocation",
clickable: true,
draggable: true
}
function onClick(e) {
alert(this.getLatLng());
}
var marker = L.marker([17.385044, 78.486671], markerOptions).on('click',onClick);
marker.addTo(map);
map.on("click", function(e){
var mp = new L.Marker([e.latlng.lat, e.latlng.lng]).addTo(map);
alert(mp.getLatLng());
});
If you are looking to set only one marker on the map, note that you must delete the previous one.如果您只想在 map 上设置一个标记,请注意您必须删除前一个标记。
var marker;
map.on('click', function (e) {
if (marker) { // check
map.removeLayer(marker); // remove old layers
}
marker = new L.Marker(e.latlng).addTo(map); // set New Layer
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.