gpt4 book ai didi

javascript - 如何清除 map 上的所有标记?

转载 作者:行者123 更新时间:2023-12-03 00:44:35 25 4
gpt4 key购买 nike

这是一个将标记添加到 map 的函数:

function markersresult() {
a++;
var addressPoints = count;

markers = L.markerClusterGroup({
spiderfyOnMaxZoom: false,
showCoverageOnHover: false,
zoomToBoundsOnClick: true,
iconCreateFunction: function(cluster) {
return new L.DivIcon({
iconSize: new L.Point(20, 20)
});
}
});

for (var i = 0; i < addressPoints.length; i++) {
var a = addressPoints[i];
var marker = L.circleMarker(L.latLng(a[0], a[1]));
markers.addLayer(marker);
marker.on('click', function(e) {
$('#results').modal('show');
$("#results .modal-body").html("<p>1 Risultato</p><button id='goToResul' type='button' class='primary-btn'>Go to result</button>");
var curPos = e.target.getLatLng();
$("#longiTude").val(curPos.lng);
$("#latiTude").val(curPos.lat);
var lat = e.target.getLatLng().lat;
var lng = e.target.getLatLng().lng;
});
}

map.addLayer(markers);

//count = [];
markers.on('clusterclick', function(a) {
console.log('cluster ' + a.layer.getAllChildMarkers().length);
var childrenMarkerCluster = a.layer.getAllChildMarkers();
var lat = null;
var lng = null;
$.each(childrenMarkerCluster, function(ind, marker) {
var markerPosition = marker.getLatLng();
if (lat === null && lng === null) {
// store the coordinates of first marker
lat = markerPosition.lat;
lng = markerPosition.lng;
childrenMarkerInSamePosition = true;
$("#longiTude").val(lng);
$("#latiTude").val(lat);
} else {
// if it has already gotten a result false, stop the compare
if (childrenMarkerInSamePosition === false)
return;

if (markerPosition.lat !== lat || markerPosition.lng !== lng) {
childrenMarkerInSamePosition = false;
}
}
});

$('#results').modal('show');
$("#results .modal-body").html("<p>" + a.layer.getAllChildMarkers().length + " risultati</p><button id='goToResul' type='button' class='primary-btn'>Go to result</button>");
});
}

单击此按钮标记应清除:

$("#riprova").on("click", function(){
// REMOVE MARKERS
});

我不确定应该在哪里执行此操作 markers.clearMarkers(); 因为在按钮单击中给了我

markers.clearMarkers is not a function

最佳答案

$("#riprova").on("click", function(){
markers.clearLayers();
});

文档位于:

https://github.com/Leaflet/Leaflet.markercluster#adding-and-removing-markers

关于javascript - 如何清除 map 上的所有标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53303329/

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