MarkerCluster 标记仍然显示在簇后面

MarkerCluster markers are still showing behind cluster

我正在尝试将标记隐藏在我的集群后面,只有在单击标记时才会显示它们。

它是这样显示的:

然而,当我单击集群并退出它时,它又回到了它应该看起来的样子(第二张图片)

(这是我最初想要的样子)

这是我的代码:

var markers = new L.MarkerClusterGroup();



 

   
                        markers.addLayer(L.marker([currentLatitude, currentLongitude], { icon: populationIcon }).addTo(mymap).bindPopup(
                            `The population of ${thisCountry.countryName} is ${thisCountry.countryPopulation}.`));


                        markers.addLayer(capitalMarker = L.marker([currentLatitude, currentLongitude], { icon: cityIcon }).addTo(mymap).bindPopup(
                            `The capital city of ${thisCountry.countryName} is ${thisCountry.countryCapital}.`));

                        markers.addLayer(carMarker = L.marker([currentLatitude, currentLongitude], { icon: carIcon }).addTo(mymap).bindPopup(
                            `They drive on the ${thisCountry.carSide} of the road in ${thisCountry.countryName}.`));

                        //add cluster to map
                        mymap.addLayer(markers);

您也将创建的标记添加到地图,这是错误的 (icon: carIcon }).addTo(mymap).bindPopup()。

改变

 markers.addLayer(carMarker = L.marker([currentLatitude, currentLongitude], { icon: carIcon })
  .addTo(mymap)
  .bindPopup(`They drive on the ${thisCountry.carSide} of the road in ${thisCountry.countryName}.`));

 markers.addLayer(carMarker = L.marker([currentLatitude, currentLongitude], { icon: carIcon })
  .bindPopup(`They drive on the ${thisCountry.carSide} of the road in ${thisCountry.countryName}.`));