Javascript Floate.js使用绘制的项目重新渲染地图时如何删除地图中的选定图层

Javascript Floate.js使用绘制的项目重新渲染地图时如何删除地图中的选定图层,javascript,reactjs,leaflet,leaflet.draw,Javascript,Reactjs,Leaflet,Leaflet.draw,每当用户单击弹出的多边形形状层时,我尝试在地图上渲染多边形形状的曲面 显示带多边形的详细信息,可以编辑图层。在弹出窗口中,有删除多边形的选项。单击弹出窗口上的“删除”后,我尝试使用新曲面(即多边形)数据重新初始化贴图,但仍然显示选定曲面 componentDidUpdate(prevProps, prevState) { const { user, surfaces } = this.props; const { allLayers } = this.state; const

每当用户单击弹出的多边形形状层时,我尝试在地图上渲染多边形形状的曲面 显示带多边形的详细信息,可以编辑图层。在弹出窗口中,有删除多边形的选项。单击弹出窗口上的“删除”后,我尝试使用新曲面(即多边形)数据重新初始化贴图,但仍然显示选定曲面

componentDidUpdate(prevProps, prevState) {
   const { user, surfaces } = this.props;
   const { allLayers } = this.state;
   const that = this;
   let selectedSurface = null;
   if (!prevProps.user.id && user.id) {
     this.initializeMap();
   }
   if (this.props.deleteAction.success !== prevProps.deleteAction.success) {
     this.props.actionFetch();
     map.remove();
     this.initializeMap();
   }
   if ((allLayers.length === 1 && surfaces.length) || (surfaces.length !== 
      prevProps.surfaces.length)) {
     let allLayers = [{ key: -1, name: this.props.intl.formatMessage({ id: 
     'surface.allsurfaces' }), color: '#CCCCCC' }];
      surfaces.forEach((o) => {
        let l = L.geoJSON(o.geometry)._layers;
        [l] = Object.keys(l).map(ob => l[ob]);
        const customlayer = this.addPopupToLayer(o, l);
        map.addLayer(drawnItems[o.surface_type.id].addLayer(customlayer));
         l.on('click', (e) => {
          if (selectedSurface) {
          selectedSurface.editing.disable();
         }
        selectedSurface = e.target;
        e.target.editing.enable();
          that.setState({
            popup: true,
            detail: true,
            surfaceDetail: o,
            typeSelected: o.surface_type,
            editSurface: selectedSurface
          });
        });

     allLayers.push({
       key: o.surface_type.id,
       name: o.surface_type.name,
       color: o.surface_type.color
     });
   });
   allLayers = allLayers.filter(
     (l, index, self) => self.findIndex(
       t => t.key === l.key
       ) === index
     );
     this.setState({
      allLayers,
      counter: surfaces.length
    });
  }
 }

initializeMap() {
  const { user, actionFetch, actionFetchTypes } = this.props;
  actionFetch();
  actionFetchTypes();
   map = L.map('map', {
    center: [...user.airport.location.coordinates].reverse(),
    zoom: 15,
    editable: true,
   });
   L.gridLayer.googleMutant({ type: 'satellite', maxZoom: 20 }).addTo(map);

    const that = this;
    map.on(L.Draw.Event.CREATED, (e) => {
     drawnItems[that.state.typeSelected.key].addLayer(e.layer);
     utils.toggleZooming(map, 'disable');
    that.setState({ popup: true, layer: e.layer });
    });
    map.on('draw:deleted', (e) => {
      that.setState({ popup: false });
   });
 }
。在弹出窗口中,有删除多边形的选项

请检查下面的示例

//初始化映射
var map=L.map('map'{
中间:[0.4102],
缩放:7
});
//添加地图图层(OpenStreetMap)
L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png'{
maxZoom:19,
属性:“©;,由提供”
}).addTo(地图);
//加载示例GEOJSON(来自Wikipedia)
var geojsonFeature={
“类型”:“FeatureCollection”,
“特点”:[
{
“类型”:“功能”,
“几何学”:{
“类型”:“点”,
“坐标”:[102.0,0.5]
},
“财产”:{
“prop0”:“A”
}
},{
“类型”:“功能”,
“几何学”:{
“类型”:“行字符串”,
“坐标”:[
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
“财产”:{
“prop0”:“B”,
“prop1”:0.0
}
},{
“类型”:“功能”,
“几何学”:{
“类型”:“多边形”,
“坐标”:[
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
},
“财产”:{
“prop0”:“C”,
“prop1”:{“this”:“that”}
}
}
]
};
//加载要映射的GEOJSON对象/数组
L.geoJSON(geojsonFeature{
//基于特性的样式特征
风格:功能(特征){
开关(feature.properties.prop0){
案例“B”:返回{color:“red”}
案例“C”:返回{color:“green”}
}
},
//将点特征的默认生成器替换为圆
pointToLayer:功能(特性、latlng){
返回L.circleMarker(车床{
半径:14,
填充颜色:“橙色”,
颜色:“橙色”,
体重:2,
不透明度:1,
填充不透明度:0.5
});
},
//将工具提示绑定到每个要素
onEachFeature:功能(功能,图层){
var popupContent=“单击此处删除此多边形”;
if(feature.properties.prop0){
popupContent+=feature.properties.prop0;
}
layer.bindPopup(弹出内容);
layer.myTag=feature.properties.0
}
}).addTo(地图);
函数removeSelectedLayer(layerName){
map.eachLayer(函数(层){
console.log(layer.myTag)
if(layer.myTag&&layer.myTag===layerName){
map.removeLayer(图层)
}
});
}
#地图{
高度:500px;
}


谢谢您的帮助,先生。删除曲面后,我的问题有点不同。用户可以添加新曲面或编辑其他现有曲面。因此,我考虑在删除后重新初始化地图,编辑或创建操作。这样做是否正确。如果不正确,请用正确的方式指导我。@Pavankusuri删除后您不需要重新初始化映射。您可以添加到现有地图中。这对你有帮助