Leaflet 传单-添加删除GeoJSON多边形稍后按属性单击?
我在使用传单,我在研究GeoJSON,我想在点击事件中添加或删除多边形 使用传单中GeoJSON示例中的示例数据,我构建了以下内容,但我不知道如何做到这一点 我确实使用了不带geojson的多边形图层,并成功点击添加图层,但没有删除,只是重复添加图层 谢谢你的帮助Leaflet 传单-添加删除GeoJSON多边形稍后按属性单击?,leaflet,geojson,Leaflet,Geojson,我在使用传单,我在研究GeoJSON,我想在点击事件中添加或删除多边形 使用传单中GeoJSON示例中的示例数据,我构建了以下内容,但我不知道如何做到这一点 我确实使用了不带geojson的多边形图层,并成功点击添加图层,但没有删除,只是重复添加图层 谢谢你的帮助 <html> <head> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leafle
<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>
关于你的问题: 您可以使用
map.addLayer(layer)
或layer.addTo(map)
/map.removeLayer(layer)
或layer.removefom(map)
从地图或图层组中添加和删除图层
但我认为最好的方法是使用图层控制
如果要使用外部控制(如您的示例):
此外,您还可以检查地图是否有一个图层:
map.hasLayer(layer)
谢谢,我添加了一个函数(添加到原始q),但我看到一个错误“试图分配给hasLayer行的只读属性”。您必须创建自己的图层,并使用我发布的添加图层功能。您不能使用该值直接添加到地图,因为来自您的值是一个字符串add\u layer('Republican')
。创建一个共和党和一个民主党图层,就像我描述的那样,然后将这个图层添加到地图中。
function add_layer(value){
if(map.hasLayer(value)) {
map.removeLayer(value);
}else{
map.addLayer(value);
}
}
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);
}
}