首页 文章

React-Leaflet-Draw:在保存时访问多边形的坐标数组

提问于
浏览
1

我有一个组件在 Map 上放置了一个可编辑的多边形 . 当用户点击“保存”按钮时,我想访问多边形的新顶点数组,以便我可以保存它们 . 我该怎么做呢?

My component:

<FeatureGroup>
    <EditControl
        position="topright"
        onEdited={e => console.log(e)}
        edit={{ remove: false }}
        draw={{
                marker: false,
                circle: false,
                rectangle: false,
                polygon: false,
                polyline: false
             }}
        />
        <Polygon positions={polygonCoords} />;
</FeatureGroup>

我得到的几个参考文献:

https://github.com/alex3165/react-leaflet-draw

https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#l-draw-event-draw:editstop

我理解我必须实现处理onEdited钩子和由此生成的事件的某种函数,但是有没有人知道如何从这个事件中获取新的顶点数组?

1 回答

  • 2

    对于任何正在努力解决这个问题的人来说,这是ES6的可行解决方案:

    <FeatureGroup>
                <EditControl
                    position="topright"
    
                    //this is the necessary function. It goes through each layer
                    //and runs my save function on the layer, converted to GeoJSON 
                    //which is an organic function of leaflet layers.
    
                    onEdited={e => {
                        e.layers.eachLayer(a => {
                            this.props.updatePlot({
                                id: id,
                                feature: a.toGeoJSON()
                            });
                        });
                    }}
                    edit={{ remove: false }}
                    draw={{
                        marker: false,
                        circle: false,
                        rectangle: false,
                        polygon: false,
                        polyline: false
                    }}
                />
                <Polygon positions={[positions(this.props)]} />;
            </FeatureGroup>
        );
    

相关问题