在Leaflet中使用react-leaflet-draw插件创建新图层之前删除图层,可以按照以下步骤进行操作:
import React, { Component } from 'react';
import { Map, TileLayer } from 'react-leaflet';
import 'leaflet-draw';
class MapComponent extends Component {
render() {
return (
<Map center={[51.505, -0.09]} zoom={13}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="..."
/>
</Map>
);
}
}
export default MapComponent;
import React, { Component } from 'react';
import { Map, TileLayer } from 'react-leaflet';
import { EditControl } from 'react-leaflet-draw';
import 'leaflet-draw';
class MapComponent extends Component {
render() {
const drawOptions = {
position: 'topright',
draw: {
polyline: false,
polygon: true,
circle: false,
rectangle: false,
marker: false,
},
edit: {
featureGroup: this.featureGroup,
},
};
return (
<Map center={[51.505, -0.09]} zoom={13}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="..."
/>
<EditControl {...drawOptions} />
</Map>
);
}
}
export default MapComponent;
import React, { Component } from 'react';
import { Map, TileLayer } from 'react-leaflet';
import { EditControl } from 'react-leaflet-draw';
import 'leaflet-draw';
class MapComponent extends Component {
constructor(props) {
super(props);
this.featureGroup = new L.FeatureGroup();
}
render() {
// ...
}
}
export default MapComponent;
import React, { Component } from 'react';
import { Map, TileLayer } from 'react-leaflet';
import { EditControl } from 'react-leaflet-draw';
import 'leaflet-draw';
class MapComponent extends Component {
constructor(props) {
super(props);
this.featureGroup = new L.FeatureGroup();
}
handleCreated = (e) => {
const { layerType, layer } = e;
if (layerType === 'polygon') {
this.featureGroup.addLayer(layer);
}
};
render() {
const drawOptions = {
position: 'topright',
draw: {
polyline: false,
polygon: true,
circle: false,
rectangle: false,
marker: false,
},
edit: {
featureGroup: this.featureGroup,
},
};
return (
<Map center={[51.505, -0.09]} zoom={13}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="..."
/>
<EditControl onCreated={this.handleCreated} {...drawOptions} />
</Map>
);
}
}
export default MapComponent;
import React, { Component } from 'react';
import { Map, TileLayer } from 'react-leaflet';
import { EditControl } from 'react-leaflet-draw';
import 'leaflet-draw';
class MapComponent extends Component {
constructor(props) {
super(props);
this.featureGroup = new L.FeatureGroup();
}
handleCreated = (e) => {
const { layerType, layer } = e;
if (layerType === 'polygon') {
// 删除之前的图层
this.featureGroup.clearLayers();
this.featureGroup.addLayer(layer);
}
};
render() {
const drawOptions = {
position: 'topright',
draw: {
polyline: false,
polygon: true,
circle: false,
rectangle: false,
marker: false,
},
edit: {
featureGroup: this.featureGroup,
},
};
return (
<Map center={[51.505, -0.09]} zoom={13}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="..."
/>
<EditControl onCreated={this.handleCreated} {...drawOptions} />
</Map>
);
}
}
export default MapComponent;
通过以上步骤,你可以在Leaflet中使用react-leaflet-draw插件创建新图层之前删除之前的图层。这样可以确保每次只有一个图层存在,避免图层重叠或混淆的问题。
腾讯云相关产品和产品介绍链接地址:
请注意,以上链接仅供参考,具体的产品选择应根据实际需求和情况进行评估和决策。
领取专属 10元无门槛券
手把手带您无忧上云