我试图通过使用反应的状态来制作动态多边形,但传单多边形没有被重新渲染。
目标是创建用户在地图中单击创建的多边形。
class SimpleExample extends React.Component {
constructor() {
super();
this.state = {
positions: [[51.505, -0.09]]
};
}
addPosition = (e) => {
const {positions} = this.state
console.log('Adding positions', positions)
positions.push([e.latlng.lat, e.latlng.lng])
this.setState({positions})
}
render() {
console.log('Should render new positions', this.state.positions)
const staticPositions = [[51.505, -0.09], [51.4958128370432, -0.10728836059570314], [51.49602657961649, -0.09956359863281251]]
return (
<Map
center={[51.505, -0.09]}
onClick={this.addPosition}
zoom={13}
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
/>
<Polygon positions={this.state.positions} color="blue" />
<Polygon positions={staticPositions} color="red" />
<Polyline positions={this.state.positions} />
</Map>
);
}
}