如何在运行时在react-native-mapbox-gl中显示/隐藏栅格图层(可见性属性可见/无)

Khu*_*ari 5 react-native react-native-mapbox-gl

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

<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>
Run Code Online (Sandbox Code Playgroud)

在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
      }
    }
Run Code Online (Sandbox Code Playgroud)

卫星默认可见。

如何在运行时将Satellite属性的可见性设置为None,将Satellite2可见性设置为可见?

Mapbox gl:

"@mapbox/react-native-mapbox-gl": "^6.1.3"
Run Code Online (Sandbox Code Playgroud)

反应本机:

"react-native": "0.58.9",
Run Code Online (Sandbox Code Playgroud)

Khu*_*ari 4

最后我得到了解决方案:

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>
Run Code Online (Sandbox Code Playgroud)

我添加了栅格层并以编程方式切换它。