在反应组件中弄乱了Leaflet.draw样式

时间:2019-01-23 10:10:06

标签: leaflet styles leaflet-draw

在我的react项目中,我将传单用作地图库。而且我没有使用react-leaflet软件包,而是使用了原始的传单库。

现在,我想添加leaflet-draw插件,我已经安装了leaflet-draw npm软件包,并且我的设置如下:

import 'leaflet-draw'
import './leaflet-draw.less'

class GLMap extends Component {
  componentDidMount () {
    ...
    this.setUpLeafletDraw()
    this.map.on('draw:created', this.leafletDrawHandler)
  }

  leafletDrawHandler = (e) => {
    console.log('11111', e.layer._latlngs)
    const type = e.layerType
    const layer = e.layer

    if (type === 'marker') {
      layer.bindPopup('A popup!')
    }
    this.drawnItemsLayer.addLayer(layer)
  }

  setUpLeafletDraw = () => {
    // this.drawnItems is the layer contains the drawn features
    this.drawnItemsLayer = new L.FeatureGroup()
    this.map.addLayer(this.drawnItemsLayer)
    var drawControl = new L.Control.Draw({
      edit: {
        featureGroup: this.drawnItemsLayer
      }
    })
    this.map.addControl(drawControl)
  }
}

在上面的代码中,this.map是传单Dom实例。到目前为止,传单画图的功能可以正常运行。

但是问题在于工具栏的样式如下:

enter image description here

那是什么问题?

1 个答案:

答案 0 :(得分:1)

最有可能缺少leaflet-draw CSS文件,其中包含.leaflet-draw-toolbar的声明。

可以从leaflet-draw包中导入,如下所示:

import "leaflet-draw/dist/leaflet.draw-src.css";

或通过public/index.html从CDN引用,如下所示:

<link rel="stylesheet" href="https://unpkg.com/leaflet-draw@latest/dist/leaflet.draw-src.css" />

这里是a demo