如何在 运行 时间在 react-native-mapbox-gl 中 show/hide 光栅层(可见性 属性 visible/none)

How to show/hide raster layer (visibility property visible/none) at run time in react-native-mapbox-gl

我在地图初始化中设置了自定义样式url。喜欢:

<Mapbox.MapView
   styleURL="asset://mystyle.json"
   logoEnabled={false}
   attributionEnabled={false}
   ref={(e) => { this.oMap = e }}
   animate={true}
   zoomLevel={6}
   centerCoordinate={[54.0, 24.0]}
   style={{ flex: 1 }}
   showUserLocation={true}>
</Mapbox.MapView>

在mystyle.json我有两个底图如下:

 {
      "id": "Satellite",
      "type": "raster",
      "source": "Satellite",
      "layout": {
        "visibility": "visible"
      },
      "paint": {
        "raster-opacity": 1
      }
    },
 {
      "id": "Satellite2",
      "type": "raster",
      "source": "Satellite",
      "layout": {
        "visibility": "none"
      },
      "paint": {
        "raster-opacity": 1
      }
    }

卫星默认可见。

如何将卫星 属性 的可见性设置为 none 并将卫星 2 的可见性设置为在 运行 时间可见?

Mapbox gl :

"@mapbox/react-native-mapbox-gl": "^6.1.3"

反应本机:

"react-native": "0.58.9",

我可以看到您使用的是旧的贬值版本的 mapbox-gl。 此软件包已弃用,请改用 this

安装

依赖关系

node
npm
React Native recommended version 0.50 or greater

Git

git clone git@github.com:mapbox/react-native-mapbox-gl.git
cd react-native-mapbox-gl

纱线

yarn add @mapbox/react-native-mapbox-gl

Npm

npm install @mapbox/react-native-mapbox-gl --save

一切顺利!

假设我们有一个状态 isStateliteVisible:false,

当您想要可见性时,现在将此状态更改为 true

并像这样使用 mapbox,

<Mapbox.MapView
   styleURL={this.state.isStateliteVisible?...visiblityStyle:....noneStyle} // use this as per your case
   logoEnabled={false}
   attributionEnabled={false}
   ref={(e) => { this.oMap = e }}
   animate={true}
   zoomLevel={6}
   centerCoordinate={[54.0, 24.0]}
   style={{ flex: 1 }}
   showUserLocation={true}>
</Mapbox.MapView>

终于找到解决方案了:

constructor() {
   this.state = {
      lightMap: 'visible', 
      darkMap: 'none'
   };
} 

changeMap(){
   this.setState({darkMap:'visible'})
}

<MapboxGL.MapView
   styleURL="asset://mystyle.json"
   logoEnabled={false}
   attributionEnabled={false}
   ref={(e) => { this.oMap = e }}
   zoomLevel={6}
   centerCoordinate={[54.0, 24.0]}
   style={{ flex: 1 }}>

<MapboxGL.RasterSource
   id="idLightMap" 
   url="LAYERURL1"
   tileSize={256}>
   <MapboxGL.RasterLayer 
      id="idLightMap"
      sourceID="idLightMap"
      style={{visibility: this.state.lightMap}}>
   </MapboxGL.RasterLayer>
</MapboxGL.RasterSource>

<MapboxGL.RasterSource
   id="idDarkMap" 
   url="LAYERURL2"
   tileSize={256}>
   <MapboxGL.RasterLayer
      id="idDarkMap"
      sourceID="idDarkMap"
      style={{visibility: this.state.darkMap}}>
   </MapboxGL.RasterLayer>
</MapboxGL.RasterSource>

</MapboxGL.MapView>

我添加了栅格图层并以编程方式对其进行切换。