我正在尝试使用群集创建一个交互式地图,当用户选中一个框时需要显示该地图,并在再次取消选中此框时将其删除。 到目前为止一切运行良好,集群工作和一切,但我注意到一个奇怪的行为,我无法解释和修复:每次我取消选中该框并再次检查它,群集中显示的数字会增加该区域的标记量(因此当我执行“clearMarkers”时,它不会以某种方式重置为零
以下是相关功能的代码:
//Display or remove PREDICTED accident's markers.
function toDisplayPredictedAccidents ()
{
//If the checkbox is checked : Display all the PREDICTED accident's markers.
if(checkBoxPredicted.checked == true)
{
for (i = 0; i < predictedAccidentArray.length; i++)
{
marker = new google.maps.Marker
({
position: new google.maps.LatLng(predictedAccidentArray[i][1], predictedAccidentArray[i][2]),
icon : iconPredicted
});
markersPredicted.push(marker); //Put the created marker in an array.
//Create the pop-up when we click on the marker.
google.maps.event.addListener(marker, 'click', (function(marker, i)
{
return function()
{
infowindow.setContent(predictedAccidentArray[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
markerClusterPredicted = new MarkerClusterer(map, markersPredicted);
}
else
{
clearPredictedMarkers();
markerClusterPredicted.clearMarkers();
}
}
//clearMarkers and setAllMap are related and aim to REMOVE all the PREDICTED accident's markers.
function clearPredictedMarkers() {
setAllMapPredicted(null, markersPredicted);
}
function setAllMapPredicted(map, markersArray)
{
for (var i = 0; i < markersArray.length; i++)
{
markersPredicted[i].setMap(map);
}
}
任何人都知道如何解决此问题? :)
答案 0 :(得分:18)
仅适用于在互联网上搜索实际答案的任何人(因为这个问题很老):
let liveInfoUrl = URL(string: "http://192.168.1.66/api/cloud/app/liveInfo/7777")
let task = URLSession.shared.dataTask(with: liveInfoUrl! as URL) {data, response, error in
guard let data = data, error == nil else { return }
print(String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) ?? "aaaa")
}
task.resume()
这将删除markerCluster中的所有标记。
一切都可以在这里找到:https://googlemaps.github.io/js-marker-clusterer/docs/reference.html
答案 1 :(得分:4)
markerCluster.remove(myMarker);
如果您要删除标记列表,请执行以下操作:
for(var i=0; i<markers.length; i++){
markerCluster.removeMarker(markers[i]);
}
答案 2 :(得分:0)
只需在setAllMapPredicted(null,markersPredicted)
之后清空您的markersPredicted数组
function clearPredictedMarkers() {
setAllMapPredicted(null, markersPredicted);
markersPredicted = [];
}