简体   繁体   English

如何获取折线坐标 React-Leaft-Draw 插件?

[英]How to get polyline coordinates React-Leaft-Draw plugin?

I use this plugin to draw lines on Leaflet map in my project:我在我的项目中使用这个插件在 Leaflet map 上画线:

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

I want to get coordinates (lat,long) of drawn polyline, when _onCreate or later.我想在 _onCreate 或更高版本时获取绘制多段线的坐标(纬度、经度)。 How could i do this?我怎么能这样做?

I agree, the documentation is a bit lacking, so I hope this helps anyone else trying to do this.我同意,文档有点缺乏,所以我希望这可以帮助其他尝试这样做的人。 You first implement an onCreated function that is registered on the <EditControl> component.您首先实现在<EditControl>组件上注册的onCreated function。 You can then access the current layer object and its layerType from the event object that gets passed to this callback.然后,您可以从传递给此回调的event object 访问当前layer object 及其layerType

Depending on the shape type, you can access the coordinates of the shape via a the methods provided by Leaflet (eg circle ).根据形状类型,您可以通过 Leaflet 提供的方法(例如circle )访问形状的坐标。 See code below.请参阅下面的代码。

export default DrawTool({prop1, prop2) {
  
  const onCreated = (e) => {
    if (e.layerType === 'circle') {
      console.log("circle center, radius =", e.layer.getLatLng(), e.layer.getRadius())
    } else {
      // polygon & rectangle
      console.log("polygon coordinates =", e.layer.getLatLngs()) // array of LatLng objects
    } // marker or lines, etc.
    // map.addLayer(e.layer) // might need?
  }

  const onDelete = (e) => {
    // do something with e.layer
  }

  return (
    <EditControl
      position='topright'
      onCreated={onCreated}
      onDeleted={onDeleted}
      ...
    />
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM