我正在用一张传单地图来展示世界各地的火山。单击切换按钮时,将显示所有标记。当点击切换按钮时,标记应该会出现。
在这里,我想应用集群标记。它不起作用。
//Creating map options
var mapOptions = {
center: [40.4168, -3.7038],
zoom: 2,
minZoom: 2,
maxZoom: 18,
maxBounds: [
[-75, -190],
[90, 190]
],
maxBoundsViscosity: 0.5,
}
// Creating a map object
var map = new L.map('map', mapOptions);
// Add Tile Layer and add to map
L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=8vFNrApGjV6jRicu4ins').addTo(map);
//Adding geoJson data and adding the marker and popup
var geojsonMarkerOptions = {
radius: 3,
fillColor: "#FF0000",
color: "#000",
weight: 1,
opacity: 0.2,
fillOpacity: 0.5
};
//Marker Cluster
var markerClusters = L.markerClusterGroup();
for ( var i = 0; i < volcano.length; ++i )
{
var popup = volcano[i].name +
'<br/>' + volcano[i].properties. NAME_+
'<br/><b>Type:</b> ' + volcano[i].properties.TYPE_ +
'<br/><b>Location:</b> ' + volcano[i].properties.LOCATION;
var m = L.marker( [volcano[i].lat, volcano[i].lng], {icon: geojsonMarkerOptions} )
.bindPopup( popup );
markerClusters.addLayer( m );
}
map.addLayer( markerClusters );
//-Creating interactive buttons:Toggler button to show on/off worldwide volcanoes
var volcanoPoints = null;
// Create event listener for add Volcanoes Worldwide Button
document.getElementById("addButton").addEventListener("click", addVolcanoWorldwide);
// Add volcano worldwide function
function addVolcanoWorldwide() {
volcanoPoints.addTo(map);
};
function addVolcanoWorldwide() {
if (map.hasLayer(volcanoPoints)) {
removeVolcanoWorldwide();
};
volcanoPoints = L.geoJson(volcano, {
pointToLayer: function(feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions);
},
onEachFeature: volcanoSearch
}).addTo(map);
};
// Create event listener for the remove Volcanoes Worldwide Button
document.getElementById("removeButton").addEventListener("click", removeVolcanoWorldwide);
// Remove volcano worldwide function
function removeVolcanoWorldwide() {
volcanoPoints.remove(map);
};
document.getElementById("toggleButton").addEventListener("click", toggleVolcanoes);
// Toggle Volcanoes
function toggleVolcanoes() {
if (map.hasLayer(volcanoPoints)) {
removeVolcanoWorldwide();
}
else {
addVolcanoWorldwide();
}
};
发布于 2019-06-21 09:49:11
欢迎来到SO!
您的代码显示您从volcano
构建了两组独立的标记:-对于MarkerClusterGroup,作为具有经度和lng属性的对象数组-对于切换要素,作为传递给Leaflet GeoJSON图层组工厂的GeoJSON要素数组。
如果您的volcano
包含一组兼容的GeoJSON特性,那么第一组(针对MarkerClusterGroup)应该不会按照当前的编码方式工作。
如果您希望您的切换功能直接处理您的MarkerClusterGroup,那么只需将您的volcanoPoints
添加到MCG中,而不是添加到地图中。您也可以只构建一次,然后切换MCG。
https://stackoverflow.com/questions/56692610
复制相似问题