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 returned from the device. We also provide an error handler if something goes wrong.
Next, we use watchPosition()
to register an event handler that will be called when the user's position changes...