在我的 Django 应用程序中,我有一个带有两层“active_event_layer”和“inactive_event_layer”的传单地图。我可以在右上角的菜单中选择我想看到的图层。但是当页面加载时默认没有选择图层,所以为了看到图层我必须先选择它。我想要做的是默认设置一个图层,因此,当页面加载时,默认选择“活动事件”图层。
这是我的代码:
var active_event_collection = {{ active_events|geojsonfeature:"name"|safe }};
var inactive_event_collection = {{ inactive_events|geojsonfeature:"name"|safe }};
function onEachFeature(feature, layer) {
layer.bindPopup(feature.properties.name);
}
function map_init(map, options) {
var active_event_layer= L.geoJson(active_event_collection, {onEachFeature: onEachFeature})
var inactive_event_layer = L.geoJson(inactive_event_collection, {onEachFeature: onEachFeature})
var basemaps = {
"Gray scale": L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}),
Streets: L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
})
};
var overlayMaps = {
"Active events": active_event_layer,
"Inactive events": inactive_event_layer,
};
var features = L.control.layers(basemaps, overlayMaps).addTo(map);
map.fitBounds(active_event_layer.getBounds());
}