gpt4 book ai didi

传单 : How to have new marker in first click then update the latlng of marker in second click

转载 作者:行者123 更新时间:2023-12-04 13:46:41 26 4
gpt4 key购买 nike

我有要在 map 中呈现的标记列表,但我想要一个一个地显示。第一次点击我想制作新的标记。然后当我点击另一个位置时,我希望我的标记只移动到新的 latLng 而不是创建另一个标记。这是我的代码:

function (licon, coord, data) {
var self = jQuery(this);
var map = self.data("map");
var latlng = new L.LatLng(coord[0], coord[1]);
//Create Marker

if (licon) {
var leafIcon = L.icon(licon);
console.log(typeof (marker));
if (typeof (marker) === 'undefined') {
var marker = L.marker(latlng, {
icon: leafIcon,
"markerData": data,
draggable: true
});

} else {
console.log('not undefined');
map.removeLayer(marker);
marker = L.marker(latlng, {
icon: leafIcon,
"markerData": data,
draggable: true
});
}
} else {
var marker = L.marker(latlng, {
"markerData": data,
draggable: true
});

}

marker.addTo(map);
return marker;
}

最佳答案

结果的简单示例:http://jsfiddle.net/ve2huzxw/43/

var currentMarker;

map.on("click", function (event) {
if (currentMarker) {
currentMarker.setLatLng(event.latlng);
return;
}

currentMarker = L.marker(event.latlng, {
draggable: true
}).addTo(map).on("click", function () {
event.originalEvent.stopPropagation();
});
});

document.getElementById("done").addEventListener("click", function () {
currentMarker = null;
});

您还可以添加平滑过渡以显示移动到新位置的标记:

if (currentMarker) {
currentMarker._icon.style.transition = "transform 0.3s ease-out";
currentMarker._shadow.style.transition = "transform 0.3s ease-out";

currentMarker.setLatLng(event.latlng);

setTimeout(function () {
currentMarker._icon.style.transition = null;
currentMarker._shadow.style.transition = null;
}, 300);
return;
}

关于传单 : How to have new marker in first click then update the latlng of marker in second click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com