Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with React Native

You're reading from   Getting Started with React Native Learn to build modern native iOS and Android applications using JavaScript and the incredible power of React

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785885181
Length 172 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image