传单 - 添加删除 GeoJSON 多边形后点击 属性?
Leaflet - Add remove GeoJSON polygon later on click by property?
我正在使用传单并正在研究 GeoJSON,我想在点击事件中添加删除多边形。
我使用传单中的 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>
编辑:
add/remove 的新功能,其中 returns "TypeError: Attempted to assign to readonly property." 用于 map.hasLayer 行
function add_layer(value){
if(map.hasLayer(value)) {
map.removeLayer(value);
}else{
map.addLayer(value);
}
}
针对您的问题:
您可以使用 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)
我正在使用传单并正在研究 GeoJSON,我想在点击事件中添加删除多边形。
我使用传单中的 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>
编辑: add/remove 的新功能,其中 returns "TypeError: Attempted to assign to readonly property." 用于 map.hasLayer 行
function add_layer(value){
if(map.hasLayer(value)) {
map.removeLayer(value);
}else{
map.addLayer(value);
}
}
针对您的问题:
您可以使用 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)