A month of React Native: part #5

Getting location information

This post is part of a series, where I document my experiences learning React Native while building my first-ever mobile app. This is also my way of giving back to the React Native community, with tips and insights I note along the journey.

For most mobile apps, getting location information is a pretty common scenario. This is exactly what we’ll cover in this part of the series. Luckily, the Expo toolset makes this extremely simple, as we’ll soon see.

Getting the user’s permission

Before we are able to poll for the device’s location data, we need to ask the user to give us permission to do so. This is done in a pretty straightforward way using the Permissions API in Expo.

Fetching location

Now that we got permission, time to issue the request for the location. It’s just a couple more lines of code. Let’s see what that looks like.

A small note

You may notice that the first check in componentWillMount is to detect whether we’re running in an Android emulator. This is because the Location API doesn’t work on the Android emulator — you need to either use the iPhone simulator or on a physical device.

What’s next?

The Location API has a few quite useful methods that we didn’t show in this simple example. For instance, you can subscribe to location updates from the device either when the app is running in the foreground or when it’s running in the background. You can also convert addresses to geolocation coordinates or vice versa and do other cool things.

That’s it for now. Pretty short post this time, but with a ton of possibilities! Imagine — a service like Uber or Lyft only became viable when we had a portable computer with an embedded GPS receiver (i.e. a smartphone). Next up: we’ll see how we can notify the user of events using push notifications.

If you enjoyed this article, feel free to hit that clap button 👏 to help others find it.

About me

I’m Alex, a software engineer and startup geek. Currently, I’m a co-founder at a startup called AgentRisk, where I’m the VP of Product & Engineering. In a past life, I was part of the founding engineering team at an enterprise cloud storage startup in Silicon Valley, did a bunch of cool cutting-edge projects at Cisco Research, and worked on some really innovative data center network architectures while doing my Master’s at UCSD. I always enjoy learning new technologies and b̵u̵i̵l̵d̵i̵n̵g̵ shipping side-projects all the time.

Co-founder & CTO @ AgentRisk. Former infra-tech guy (storage, networks). Startup nerd. Always building cool side-projects. #LongLA

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store