如何在 React-Mapbox-GL 中获取当前缩放?

How to get current zoom in React-Mapbox-GL?

如何在 React mapbox gl 中获取当前地图的缩放比例?

包裹:https://github.com/alex3165/react-mapbox-gl/tree/0df46b1dcf194cdf140638f653221d8a82f0b195

我已经尝试使用 "How to start" 中的示例在 Map 上使用 getZoom 方法,但它不起作用。我有错误 Map.getZoom 不是函数。

编辑 ================

当我尝试通过 refs 访问它时,我遇到了屏幕截图中的问题。如何获得具有我的 getZoom 功能的 .state.map.e?

您正在使用的 Map 是一个 ReactMapboxGl 对象,它是 mapbox-gl-js 的包装器。您想在 mapboxgl.Map 对象上调用 getZoom()

To use the original Mapbox API use onStyleLoad property, the callback function will receive the map object as a first arguments, then you can add your own logic alongside react-mapbox-gl. [source]

因此您可以从以下位置获取:

<ReactMapboxGl onStyleLoad={ el => this.map = el } />

那么你可以在这里使用它:

componentDidMount() {
    const currentZoom = this.map.getZoom();
}