我想创建一个按钮,其功能与传单地图中的工具栏相同。按下该按钮时,用户可以在地图上的onCreate函数上绘制图形,但是我不知道如何使用此选项绘制按钮来绘制多边形。
这是我初始化传单地图的组件代码:
export class Dashboard extends React.Component {
constructor(props) {
super(props);
this.state = {
filenames: [],
downloadURLs: [],
lat: 42.696295,
lng: 23.303643,
zoom: 10,
image: null,
url: "",
progress: 0,
files: [],
uploadValue: 0,
filesMetadata: [],
rows: [],
};
}
//Set location when the map is visualized
mapRef = createRef();
onCreate = (e) => {
var layer = e.layer;
console.log("Polygon Cordinates", layer.getLatLngs());
console.log("Log_Create_Shape: ", e);
var drawedCord = layer.toGeoJSON().geometry.coordinates;
for (const result of drawedCord) this.props.saveData(result);
console.log("shape1", drawedCord);
var data = layer.toGeoJSON();
var convertedData = "text/json;charset=utf-8," + JSON.stringify(data);
console.log(convertedData);
var FileSaver = require("file-saver");
var blob = new Blob([convertedData], { type: "text/plain;charset=utf-8" });
FileSaver.saveAs(blob, "cordinates.json");
};
componentDidMount() {}
render() {
return (
这是我的地图:
<div id="map" className="dashboard container">
<br />
<HorizontalLinearStepper />
<br />
<hr />
<button>Draw polygon</button>
<br />
<Map
style={{ height: "50vh" }}
center={position}
zoom={13}
onClick={this.handleClick}
onCreate={this.onCreate}
onLocationfound={this.handleLocationFound}
>
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
/>
<FeatureGroup>
<EditControl
position="topleft"
onEdited={this._onEditPath}
onCreated={this.onCreate}
onDeleted={this._onDeleted}
onMounted={this._mounted}
onEditStart={this._onEditStart}
onEditStop={this._onEditStop}
onDeleteStart={this._onDeleteStart}
onDeleteStop={this._onDeleteStop}
draw={{
rectangle: false,
}}
/>
</FeatureGroup>
<Polygon color="purple" positions={polygon} />
<GeoJSON
data={london_postcodes}
style={this.geoJSONStyle}
onEachFeature={this.onEachFeature}
/>
</Map>
}
我遇到了完全相同的问题,并在检查了this post之后提出了解决方案>
因此,在多边形的情况下,您的绘制按钮应具有如下的onClick事件: