Javascript 从传单地图中删除指定图层

Javascript 从传单地图中删除指定图层,javascript,leaflet,Javascript,Leaflet,我有多个geojson层,只需要从地图中删除一些指定的层 dropdownlayers["12"] = { name:"Road Network", layer:L.geoJson(0,{style:style_road}), url:"http://localhost:8020/geoserver/burgula/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=burgula:burgul_roads&maxFea

我有多个geojson层,只需要从地图中删除一些指定的层

dropdownlayers["12"] = { name:"Road Network", layer:L.geoJson(0,{style:style_road}), url:"http://localhost:8020/geoserver/burgula/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=burgula:burgul_roads&maxFeatures=3500&outputFormat=application%2Fjson" } dropdownlayers["13"] = { name:"Cadastral Map", layer:L.geoJson(0,{style:style_cadastral_map,onEachFeature:label_cm}), url:"http://localhost:8020/geoserver/burgula/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=burgula:burgula_cadastral_boundary&maxFeatures=3500&outputFormat=application%2Fjson" } function load_layer(dropdownchoice) { $.ajax({ type: "GET", dataType: "json", jsonpCallback: 'getJson', async: true, url: dropdownchoice.url, success: function(data) { var layer = dropdownchoice.layer; //this function remove all geojson layers but i want only specified layers remove// map.eachLayer(function(layer){ if(layer instanceof L.Layer) map.removeLayer(layer) }); layer.addTo(map) $(data.features).each(function(key, data) { layer.addData(data); }) layerControl.addOverlay(layer, dropdownchoice.name); map.fitBounds(layer.getBounds()); } }).error(function() {}); } 下拉层[“12”]={ 名称:“道路网”, 图层:L.geoJson(0,{style:style_road}), url:“http://localhost:8020/geoserver/burgula/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=burgula:burgul_roads&maxFeatures=3500&outputFormat=application%2Fjson" } 下拉层[“13”]={ 名称:“地籍图”, 图层:L.geoJson(0,{style:style\u地籍图,onEachFeature:label\u cm}), url:“http://localhost:8020/geoserver/burgula/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=burgula:burgula_cadastral_boundary&maxFeatures=3500&outputFormat=application%2Fjson" } 功能加载层(下拉选择){ $.ajax({ 键入:“获取”, 数据类型:“json”, jsonpCallback:'getJson', async:true, url:dropdownchoice.url, 成功:功能(数据){ var layer=dropdownchoice.layer; //此函数用于删除所有geojson层,但我只希望删除指定的层// map.eachLayer(函数(层){ if(L.layer的层实例) map.removeLayer(图层) }); layer.addTo(映射) $(data.features)。每个(函数(键、数据){ 图层.添加数据(数据); }) layerControl.addOverlay(layer,dropdownchoice.name); map.fitBounds(layer.getBounds()); } }).error(函数(){}); }
因此,您故意迭代所有层以逐个删除它们。您能简单地使用map.removeLayer(dropdownchoice.layer)吗?谢谢您的回复。我只想删除指定的层,如示例;layer==“somename或number或来自选项值”。