我正在使用传单,正在查看GeoJSON,它的ID喜欢添加有关单击事件的多边形。
使用单张ive中的GeoJSON示例中的示例数据构建了以下内容,但我不知道该怎么做。
我确实使用了没有geojson的多边形图层,并且确实设法单击添加了一个图层,但是并没有删除它,而是反复添加了图层
感谢您的帮助
<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
</head>
<body>
<a href="javascript:add_layer('Republican')">Republican</a> | <a href="javascript:add_layer('Democrat')">Democrat</a>
<div id="map" style="height:700px;"></div>
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<script type="text/javascript">
var map = L.map('map').setView([40, -97.], 5);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox/streets-v11',
accessToken: 'x'
}).addTo(map);
var states = [{
"type": "Feature",
"properties": {"party": "Republican"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
}, {
"type": "Feature",
"properties": {"party": "Democrat"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]
}
}];
L.geoJSON(states);
function add_layer(value){
if map.contains.feature.properties.party.case(value) {
map.layer.remove(feature.properties.party.case(value))
} else {
feature.properties.party.case(value.addTo(map))
}
}
</script>
</body>
</html>
编辑: 添加/删除的新函数,该函数返回“ TypeError:尝试分配给只读属性”。 map.hasLayer行
function add_layer(value){
if(map.hasLayer(value)) {
map.removeLayer(value);
}else{
map.addLayer(value);
}
}
答案 0 :(得分:1)
对于您的问题:
您可以使用map.addLayer(layer)
或layer.addTo(map)
/ map.removeLayer(layer)
或layer.removeFrom(map)
从地图或图层组添加和删除图层。
但是我认为最适合您的是使用图层控制https://leafletjs.com/examples/layers-control/
var states = [{
"type": "Feature",
"properties": {"party": "Republican"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-104.05, 48.99],
[-97.22, 48.98],
[-96.58, 45.94],
[-104.03, 45.94],
[-104.05, 48.99]
]]
}
}, {
"type": "Feature",
"properties": {"party": "Democrat"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-109.05, 41.00],
[-102.06, 40.99],
[-102.03, 36.99],
[-109.04, 36.99],
[-109.05, 41.00]
]]
}
}];
var democrat = L.geoJSON(states, {
filter: function(feature, layer) {
return feature.properties.party === "Democrat";
}
});
var republican = L.geoJSON(states, {
filter: function(feature, layer) {
return feature.properties.party === "Republican";
}
});
var overlayLayers= {
"Republican": republican,
"Democrat": democrat
};
L.control.layers(null,overlayLayers).addTo(mymap);
如果您想使用外部控件(例如您的示例):
function add_layer(value){
map.removeLayer(republican);
map.removeLayer(democrat);
if(value === "Republican"){
map.addLayer(republican);
}else{
map.addLayer(democrat);
}
}
您还可以检查地图上是否包含以下图层:map.hasLayer(layer)