0

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?

enter image description here

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>
    );
  }
}
0

2 Answers 2

1

The first time you render this.state.lastPosition is null and all hell breaks lose. An exception is thrown and react breaks. You only set the lastPosition to correct data once it arrives.

You need to put a check at the point you are trying to get coords.

<MapView
          style={styles.map}
          initialRegion={{
            latitude: this.state.lastPosition ?
 this.state.lastPosition.coords.latitude : 0,
            longitude: this.state.lastPosition ?
 this.state.lastPosition.coords.longitude : 0,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}
        />

You probably want to show a loader while you get the data and not set it to zero. But I hope you get the point.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm stilling getting the error: undefined is not an object (evaluating 'this.state.lastPosition.coords.latitude')
As Avi said in his answer, do you want to store the string of the json returned (you use JSON.stringify)? Just set it to position
I've removed JSON.stringify() and the map shows, but it's using 0 for the latitude and longitude.
The only reason can be that lastPosition did not get set to an object in navigator.geolocation.watchPosition callback. You need to debug from here on.
0

You are using JSON.stringify(position) which converts the object to a string and then you store it in the state as a string.

Are you sure that this is what you wanted to do? Because that explains why coords is undefined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.