如何在React-Mapbox-GL中获得当前缩放? [英] How to get current zoom in React-Mapbox-GL?

查看:217
本文介绍了如何在React-Mapbox-GL中获得当前缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i get current zoom of map in react mapbox gl?

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

我已经尝试过如何开始"中的示例,以在Map上使用getZoom方法,但是它不起作用.我有一个错误,认为Map.getZoom不是一个函数.

I have tried on example from "How to start" to use getZoom method on Mapbut it's not working. I have error that Map.getZoom is not a function.

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

EDIT ================

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

When i try to access it by refs then i have problem like on screenshot. How to get this .state.map.e which have my getZoom function?

推荐答案

您正在使用的MapReactMapboxGl对象,它是mapbox-gl-js的包装.您要在 getZoom() >对象.

The Map you are using is a ReactMapboxGl object which is a wrapper of mapbox-gl-js. You want to call getZoom() on the mapboxgl.Map object.

要使用原始的Mapbox API,请使用onStyleLoad属性,回调函数将接收地图对象作为第一个参数,然后您可以在react-mapbox-gl旁边添加自己的逻辑. [源代码]

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]

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

So you can get it from:

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

然后您可以在这里使用它:

Then you could use it here:

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

这篇关于如何在React-Mapbox-GL中获得当前缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆