r3w*_*3wt 5 leaflet reactjs react-leaflet react-leaflet-draw leaflet-draw
我正在尝试将一些 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
]
]
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试将此数据导入到的代码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));
})
}
}
Run Code Online (Sandbox Code Playgroud)
我可能做错了什么(或者更好的是,我可以通过任何方式实现这一点)?
希望这对你有帮助。首先,不建议在_onFeatureGroupReady中使用this.setState,它会导致地图多次渲染。可以将其转移到在地图渲染之前调用的componentDidMount。关于_onFeatureGroupReady中的返回,它是不完全是一个错误,但它会返回未定义。
_onFeatureGroupReady = (ref) => {
console.log('_onFeatureGroupReady');
console.log(ref);
if(ref===null) {
return;
}
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');
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));
}
}
Run Code Online (Sandbox Code Playgroud)
然后,关于地图。中心坐标和坐标中的经纬度是相反的。所以,可能你在 getGeoJson() 中设置的坐标是错误的。
<Map center={[37.79,-122.45]} zoom={13} zoomControl={false}>
<FeatureGroup ref={ (reactFGref) => {this._onFeatureGroupReady(reactFGref);} }>
...
</FeatureGroup>
</Map>
Run Code Online (Sandbox Code Playgroud)
函数 getGeoJson():
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-122.45,
37.79
],
[
-122.42999999999999,
37.79,
],
[
-122.42999999999999,
37.77
],
[
-122.45,
37.77
],
[
-122.45,
37.79
]
]
]
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4058 次 |
| 最近记录: |