Leaflet React在功能组件中获取地图实例
问题描述
我想在地图外有一个按钮,可以将视图更改为另一个坐标.
I want to have a button outside the map that changes the view to another coordinates.
有没有办法让 mapContainer 实例调用它们的函数?或者我该如何实现这个功能?
Is there any way to get mapContainer instance to call their functions? Or how can I implement that function?
我试图通过使用 ref 来获取它,但它不起作用.这是我当前的代码
I tried to get it by using ref, but it's not working. Here is my current code
我正在使用 react-leaflet v3
I'm using react-leaflet v3
推荐答案
你需要使用一个包含你的按钮的组件.要获取地图实例,请使用 MapContainer
的 whenCreated
属性.我认为 mapRef
在最新版本中不再有效.
You need to use a component which will include your button inside. To take the map instance use whenCreated
prop of MapContainer
. I think mapRef
is not valid anymore with the latest version.
地图容器:
使用按钮及其事件创建组件
Create the component with the button and its event
演示
这篇关于Leaflet React在功能组件中获取地图实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!