首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输入geojson以作出反应-绘制单张

输入geojson以作出反应-绘制单张
EN

Stack Overflow用户
提问于 2018-10-07 01:18:37
回答 1查看 3.6K关注 0票数 4

我正在尝试将一些GeoJSON导入到FeatureGroup in _onFeatureGroupReady事件处理程序中,但它似乎没有呈现到映射中。代码主要基于库react-leaflet-draw 这里的示例。奇怪的是,编辑菜单变得可用,表明数据可能在那里,但只是没有呈现。

我不知道发生了什么,因为我一般都是地图初学者。相关代码位于else if(this.props.data) {块中。console.log()语句都以正确的格式显示数据的存在。

代码语言:javascript
复制
{
  "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的代码

代码语言:javascript
复制
_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));
        })
    }

}

我可能做错了什么(或者更好,我可以实现这一点)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-26 08:49:33

希望这将对you.First有所帮助,不建议在_onFeatureGroupReady中使用this.setState,它将导致map.May中的多个呈现将其传输到componentDidMount,在映射rendered.And之前调用componentDidMount,关于_onFeatureGroupReady中的返回,它并不完全是一个错误,但它将返回未定义的。

代码语言:javascript
复制
_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));
    }
}

然后,关于中心坐标和坐标中的Map.The纬度和经度是opposite.So,您在getGeoJson()中设置的坐标可能是错误的。

代码语言:javascript
复制
<Map center={[37.79,-122.45]} zoom={13} zoomControl={false}>
        <FeatureGroup ref={ (reactFGref) => {this._onFeatureGroupReady(reactFGref);} }>
            ...
        </FeatureGroup>
      </Map>

函数getGeoJson():

代码语言:javascript
复制
  {
    "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    
            ]
        ]
      ]
    }
}

这是我的结果。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52684688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档