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

两盒软妹~` 提交于 2019-12-23 09:59:08

问题


I have set custom style url in map initialization. Like :

<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>

In mystyle.json I have two base map as below :

 {
      "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
      }
    }

Satellite is visible default.

How to set visibility of satellite property to none and satellite2 visibility to visible at run time?

Mapbox gl :

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

React native :

"react-native": "0.58.9",

回答1:


let say we have one state isStateliteVisible:false,

now change this state to true when you want to visibility

and use mapbox like this,

<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>



回答2:


Finally I got solution :

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>

I have added raster layer and programmatic toggling it.




回答3:


I can see that you are using a older depreciated version of mapbox-gl. This package is deprecated please use this instead.

Installation

Dependencies

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

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

Npm

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

You're good to go!



来源:https://stackoverflow.com/questions/55036420/how-to-show-hide-raster-layer-visibility-property-visible-none-at-run-time-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!