我正在尝试将一些 GeoJSON 导入到FeatureGroup
事件_onFeatureGroupReady
处理程序中,但它似乎没有呈现到地图中。代码主要基于react-leaflet-draw
此处库中的示例。奇怪的是,编辑菜单变得可用,表明数据可能在那里,但只是没有被渲染。
我不确定发生了什么,因为我一般是地图的初学者。相关代码在else if(this.props.data) {
块中。这些console.log()
语句都显示了数据的存在并且格式正确。
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
37.79,
-122.45
],
[
37.79,
-122.42999999999999
],
[
37.77,
-122.42999999999999
],
[
37.77,
-122.45
],
[
37.79,
-122.45
]
]
]
}
}
]
}
这是我尝试将此数据导入的代码FeatureGroup
:
_onFeatureGroupReady = (ref) => {
console.log('_onFeatureGroupReady');
console.log(ref);
if(ref===null) {
return;//bug???
}
this._editableFG = ref;
// populate the leaflet FeatureGroup with the geoJson layers
if(this.state.data) {
console.log('importing service area from state');
let leafletGeoJSON = new L.GeoJSON(this.state.data);
let leafletFG = this._editableFG.leafletElement;
leafletGeoJSON.eachLayer( layer =>leafletFG.addLayer(layer));
}else if(this.props.data) {
console.log('importing service area from props');
this.setState({data:this.props.data},()=>{
console.log(this.state.data);
let leafletGeoJSON = new L.GeoJSON(this.state.data);
console.log(leafletGeoJSON);
let leafletFG = this._editableFG.leafletElement;
console.log(leafletFG);
leafletGeoJSON.eachLayer( layer =>leafletFG.addLayer(layer));
})
}
}
我可能做错了什么(甚至更好,我可以通过任何方式实现这一点)?