Skip to content
tezvyn:
Menu

Getting a one-time GPS location

Source: interviewEasyHow cards are made

Summary

One-shot location retrieval.

Key points

ensure permission, then call a getCurrentPosition wrapped in a promise (or Expo Location.getCurrentPositionAsync) with await inside try/catch, reading coords.latitude and longitude.

What's really being asked

Whether you can retrieve a single location fix with proper async handling, including the prerequisite permission and the failure cases.

The full answer

You first confirm location permission is granted, since a fix request without it fails or returns nothing. For the library, in an Expo app you use expo-location's Location.getCurrentPositionAsync, which already returns a promise, so you simply await it. In a bare app the common choice is react-native-geolocation-service or the community geolocation module, whose getCurrentPosition takes success and error callbacks; you wrap it in a new Promise so you can await it. You pass options such as desired accuracy and a timeout. You read the result from position.coords, specifically latitude and longitude. The whole call sits in a try/catch so timeouts, location-services-off, or denied permission surface as caught errors rather than unhandled rejections. You also distinguish a one-time getCurrentPosition from watchPosition, which subscribes to continuous updates and must be cleared.

The mistakes people make

Forgetting to request or verify permission first. Using watchPosition and never unsubscribing when only one reading is needed. Ignoring the error callback so a timeout becomes an unhandled rejection. Not setting a timeout, so the await can hang on a cold GPS. Reading latitude off the position object directly instead of position.coords.

What usually comes next

How do you tune accuracy versus battery and speed, why might the first fix be slow, how do you handle the location-services-disabled case, and how does this differ from continuous tracking.

A concrete example

With Expo: after ensuring permission, try { const pos = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High }); const { latitude, longitude } = pos.coords; } catch (e) { handle the timeout or disabled-services error }. With the callback API you would write a helper that returns new Promise((resolve, reject) => Geolocation.getCurrentPosition(resolve, reject, options)) and await that helper the same way.

Interview question

For fetching the user's location exactly once, why is getCurrentPosition preferred over watchPosition?

  • a.getCurrentPosition returns a single fix, whereas watchPosition subscribes to ongoing updates that must be unsubscribedCorrect
  • b.getCurrentPosition is the only one that works with async/await
  • c.watchPosition cannot return latitude and longitude
  • d.watchPosition does not require location permission
Why?

getCurrentPosition resolves once with a single reading, while watchPosition starts a continuous subscription you must clear, which is wasteful for a one-time need. Both require permission and both report coordinates.

Just read this? Test yourself on what you have been reading.

Read the original → github.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react-native — each one lists the topics its interview covers.

See open roles