javascript - 如何使用 react-native-maps 根据缩放值调整在 map 上绘制的形状的大小

标签 javascript google-maps react-native react-native-maps

我正在使用 react-native-maps 将 map 集成到我的应用程序中。我想显示某种雷达朝向用户前进的方向,我想覆盖不同距离的区域,如 1 公里、5 公里和 10 公里。结果如下图所示。

The desire result

我最初的解决方案是使用具有绝对位置的 View 绘制形状, 我通过这种方法得到了类似的东西。

enter image description here

上述方法的问题是 View 高度是静态的,它不响应 map 的缩放值,以及 View 高度的计算距离很重要。

我的第二个解决方案是围绕一定半径的用户标记绘制圆圈,而不是一个完整的圆圈,我可以创建半圆或半半圆来达到期望的结果。但是我找不到任何方法来使用库 react-native-maps 绘制半圆。

有什么建议可以实现我想要的结果吗?

最佳答案

你的第一个解决方案对我来说似乎已经足够好了,也许稍作修改就可以得到你正在寻找的结果,请继续:

  1. 首先让我们关联所有相关的 <View />样式化 Prop 以说明值(value)
// ... all your imports and other code

state ={
    viewHeight: 0, // or any default value you want
    viewWidth: 0, // or any default value you want

    // ... other view parameters used and other state values
}

// ... functions and other code

render() {
    return (
        // ... other rendered components etc

        <View
            style={{
                width: this.state.viewWidth,
                height: this.state.viewHeight
            }} />
    )
}
  1. 接下来我们将在每次更改时获取 map 缩放级别并执行某种条件
<MapView
    ref='map'                                       // <==== add the ref map so we can access the zoom level from our _onMapChange function later
    style={{ flex: 1 }}
    provider={PROVIDER_GOOGLE}                      // <==== this approach will not work on apple maps as the camera will not give us the zoom parameter so use google maps
    onRegionChange={() => this._onMapChange()} />   // <==== this will tell the map to fire the _onMapChange() function everytime there is a change to the map region
  1. _onMapChange()功能:
_onMapChange = async () => {
    const {
        zoom,
        pitch,
        center,
        heading 
    } = await this.refs.map.getCamera();

    // Now set the conditions for the view style based on the zoom level, maybe something like this:

    if (zoom <= 5) {
        this.setState({
            viewWidth: 50,  // what ever width you want to set
            viewHeight: 50  // what ever height you want to set
            // ... other view parameters
        })
    } else if (zoom <= 10) {
        this.setState({
            viewWidth: 100,  // what ever width you want to set
            viewHeight: 100  // what ever height you want to set
            // ... other view parameters
        })
    }
}

注意:我正在使用我的编辑器,仅此而已,因此可能存在一些语法错误

希望对您有所帮助!

关于javascript - 如何使用 react-native-maps 根据缩放值调整在 map 上绘制的形状的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58470250/

相关文章:

image - 如何将 subview 放置在 React-Native 图像组件的右下角?

javascript - 从 parent div出来时如何裁剪图像?

c# - 绑定(bind)javascript函数

javascript - 地理编码器和国家代码

ios - iPhone 3.0 SDK 中的 MAP 使用有哪些限制?

android - 用于图像上传和裁剪的 React-Native 库?(同时支持 android 和 ios)

javascript - 我刚刚添加了 var React = require ('react-native' );我的整个构建失败了

javascript - 如何访问整个项目的 angularjs sessionStorage 值?

javascript - 在 Bootstrap 3.0 中从下拉菜单中切换元素

javascript - CFGMap Google map API V3 始终以经度/纬度为中心 map