简体   繁体   English

隐藏标记 - Mapbox

[英]Hide Markers - Mapbox

I have added markers as given in example below. 我添加了标记,如下例所示。

for (let x = 0; x < mapMarkers.length; x++) {
  //mapObject.totalMarkers.push(createMarker(mapMarkers[x].whereLat, mapMarkers[x].whereLng, mapMarkers[x].id, detailContId));
  markerClusters.addLayer(createMarker(mapMarkers[x].whereLat, mapMarkers[x].whereLng, mapMarkers[x].id, detailContId));

}

markerClusters.on('clusterclick', function(a) {
  zoomLevel = mapObject.getZoom();
  if (zoomLevel < appConfig.userSetting.maxZoom) {
    mapObject.zoomIn();
  } else {
    a.layer.spiderfy();
  }
});

mapObject.addLayer(markerClusters);

Now I want to hide some markers. 现在我想隐藏一些标记。 I have done some Rnd like given below but not able to succeed. 我已经完成了下面给出的一些Rnd,但是没能成功。 Any help is great for me. 任何帮助对我来说都很棒。

objMap.removeLayer(mapMarkers[0]); 
objMap.removeLayer(mapMarkers[0]); 

This would make sense if you were adding mapMarkers to the map, but as this code shows, that isn't the case: mapMarkers is the data that you transform into marker objects using the createMarker method before adding to the map. 如果要将mapMarkers添加到地图中,这将是有意义的,但正如此代码所示,情况并非如此:mapMarkers是在添加到地图之前使用createMarker方法转换为标记对象的数据。

Conceptually, you have mapObject , that contains markerClusters , that contains markers (which you aren't assigning variable names to). 从概念上讲,你有mapObject ,包含markerClusters ,包含标记(你没有指定变量名)。 So, if you want to remove a specific marker from the markerclusters, you need to call removeLayer from the cluster's perspective, not from the map's. 因此,如果要从markerclusters中删除特定标记,则需要从集群的角度调用removeLayer ,而不是从地图中调用removeLayer

So you can use markerCluster.eachLayer , like 所以你可以使用markerCluster.eachLayer

markerClusters.eachLayer(function (layer, i) {
  if (i == 0) markerClusters.removeLayer(layer);
});

That would remove the first layer. 这将删除第一层。 If you have some other criteria for which cluster you want to remove, you'll need to either store the markers in an array before adding them to the cluster, or change that simple if statement into something that tests whether the marker should be removed. 如果您要删除要删除的群集的其他条件, 需要先将标记存储在数组中,然后再将其添加到群集中,或者将该简单的if语句更改为测试是否应删除标记的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM