Skip to content

Instantly share code, notes, and snippets.

@mellet
Created February 2, 2018 21:17
Show Gist options
  • Save mellet/ca0abbbeee65c59f5a2a920dd5aad054 to your computer and use it in GitHub Desktop.
Save mellet/ca0abbbeee65c59f5a2a920dd5aad054 to your computer and use it in GitHub Desktop.
import React from 'react'
import { MapView, FileSystem, Constants } from 'expo'
import { Button } from 'react-native-elements'
export default class App extends React.Component {
state = {
urlTemplate: 'http://c.tile.openstreetmap.org/{z}/{x}/{y}.png',
offlineUrlTemplate: `${FileSystem.documentDirectory}tiles/{z}/{x}/{y}.png`,
isOffline: false,
mapRegion: undefined
}
render() {
const { isOffline } = this.state
const urlTemplate = isOffline
? this.state.offlineUrlTemplate
: this.state.urlTemplate
return (
<View style={styles.container}>
<View style={styles.actionContainer}>
<Button
raised
borderRadius={5}
title={isOffline ? 'Go online' : 'Go offline'}
onPress={() => {
isOffline
? this.setState({ isOffline: false })
: this.setState({ isOffline: true })
}}
/>
</View>
<MapView
style={{ flex: 1 }}
onRegionChange={(mapRegion) => this.setState({mapRegion}) }>
<MapView.UrlTile urlTemplate={urlTemplate} zIndex={1} />
</MapView>
</View>
)
}
}
const styles = StyleSheet.create({
actionContainer: {
flexDirection: 'row',
padding: 15,
paddingTop: Constants.statusBarHeight + 15,
zIndex: 999,
justifyContent: 'space-around',
position: 'absolute',
top: 0,
left: 0,
right: 0
},
container: {
flex: 1
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment