I have a simple component in which I'm trying to pass my current latitude and longitude into MapView.
If I log out lastPosition I get the following:
{
"coords": {
"speed": -1,
"longitude": -122.239853,
"latitude": 37.235444,
"accuracy": 5,
"heading": -1,
"altitude": 0,
"altitudeAccuracy": -1
},
"timestamp" 235436345433454
}
So I know it's working, but why does it say null when it hits MapView in my render function?
export default class Map extends Component {
constructor (props) {
super (props)
this.state = {
initialPosition: {
coords: {
longitude: 0,
latitude: 0
}
},
lastPosition: null,
error: null
}
}
watchID: ?number = null
componentDidMount () {
navigator.geolocation.getCurrentPosition(
(position) => {
var initialPosition = JSON.stringify(position)
this.setState({initialPosition})
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
)
this.watchID = navigator.geolocation.watchPosition(
(position) => {
var lastPosition = JSON.stringify(position)
this.setState({lastPosition})
}
)
}
componentWillUnmount () {
navigator.geolocation.clearWatch(this.watchID)
}
render () {
return (
<View style={styles.container}>
<MapView
style={styles.map}
initialRegion={{
latitude: this.state.lastPosition.coords.latitude,
longitude: this.state.lastPosition.coords.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>
</View>
);
}
}
