O'Reilly logo

Getting Started with React Native by Tom Bray, Ethan Holmes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Tagging notes with geolocation

Let's take geolocation for a spin and start capturing the user's location when they save a new note. Since we're going to be using the location data when we save notes, we'll add our code to the ReactNotes component in index.ios.js or index.android.js. Let's begin by adding a function called trackLocation():

class ReactNotes extends React.Component {
  trackLocation() {
    navigator.geolocation.getCurrentPosition(
      (initialPosition) => this.setState({initialPosition}),
      (error) => alert(error.message)
    );
    this.watchID = navigator.geolocation.watchPosition((lastPosition) => {
      this.setState({lastPosition});
    });
  }

 …
}

Here we call getCurrentPosition and provide a callback that will update the current state with the position information ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required