leaflet - 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
是leaflet Dom 实例。到目前为止,传单绘制的功能可以正常工作。
但问题是工具栏的样式混乱如下:
所以有什么问题?
解决方案
很可能 leaflet-draw
缺少包含.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" />
这是一个演示
推荐阅读
- powershell - powershell中的git命令(带grep)
- docker - Gitlab Ci 配置 IPV6
- java - Jar 服务的日志文件创建问题
- java - 使用 sha256 的 Java 和 Go 哈希值不同
- reactjs - React Native Navigation 5 导入“元素类型无效”
- visual-studio-code - Gitlab,在降价中,我如何使用除标题之外的锚?
- javascript - 续集警告:`resolve/lib/core` 已弃用;请直接使用`is-core-module`
- mysql - Docker:将正在运行的容器公开给主机
- python - 使用熊猫格式化日期时间列
- laravel - 如何使用 Laravel 7 删除数据库中的数据记录