question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Location.getCurrentPositionAsync not working on Android

See original GitHub issue

expo: v22.0.0 Android: 6.0.1

export const getLocationAsync = async () => {
  let { status } = await Permissions.askAsync(Permissions.LOCATION);
  if (status !== 'granted') {
    throw new Error('Location permission not granted');
  }
  return await Location.getCurrentPositionAsync({ enableHighAccuracy: false, maximumAge: 15000 });
};

askAsync is working fine. But getCurrentPositionAsync will keep running forever. Setting any timeout will eventually go into Error: Location request timed out.

I have tried it in real device v6.0.1 (nexus 5). I have also tried Android 5 and 6 in simulator. No luck for all of them.

I guess this code may have some hints to it.

Actually I have been waiting for the fix since expo v18.0.0. I saw some issues somewhat related to this, but I could not find any solution. Any update on this will be very much appreciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:36 (11 by maintainers)

github_iconTop GitHub Comments

10reactions
hugogrochaucommented, Nov 20, 2018

My app depends greatly on Location Services, and after getting bug reports from some users we decided to investigate it further. We’ve mapped out some current problems, I hope it helps:

  • With enableHighAccurary: false and GPS only mode Location.getCurrentPositionAsync hangs indefinitely
  • With enableHighAccurary: true and GPS only mode Location.getCurrentPositionAsync hangs indefinitely
  • With enableHighAccuracy: true and high accuracy mode Location.getCurrentPositionAsync sometimes takes over 5 seconds to return
  • On some phones no configuration combination works. Location.getCurrentPositionAsync always errors with “Location services are disabled” or hangs indefinitely. It seems to happen mostly with Motorola phones. I was able to reproduce this on a Moto G4 device running Android 7.1.

I’ve also prepared a snack to facilitate tests. It’s configured to timeout after 5s instead of hanging indefinitely while running Location.getCurrentPositionAsync: https://snack.expo.io/@hugogrochau/location-tests

9reactions
carsonwahcommented, Apr 28, 2018

@raajnadar You are right!

screenshot_20180428-144800 screenshot_20180428-144803

If I set it to “Device only”, it halts forever at getCurrentPositionAsync(), and return timeout error, which matches the behaviour described above.

Result from Location.getProviderStatusAsync() when “Device only”:

{
  "gpsAvailable": true,
  "locationServicesEnabled": true,
  "networkAvailable": false,  // <-- This
  "passiveAvailable": true,
}

Maybe GPS on its own is useless for Expo apps, because Location.getCurrentPositionAsync() needs network to work?

Not sure if this is the source of this error. But I can now provide more instruction to user to enable the location function. Thanks!


@nikki93 I think at least this setting options can be put in Expo Location doc as the behaviour (halting forever) is unexpected and hard to debug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Location.getCurrentPositionAsync() never returns anything
This doesn't seem to work on my Android simulator despite the timeout . It just hangs indefinitely preventing further code to run. It...
Read more >
Location.getCurrentPositionAsync Example - Expo Snack
work on Sketch in an Android emulator. Try. it on your device!',. }); } else {. this._getLocationAsync();. } } _getLocationAsync = async ()...
Read more >
Do location services work in expo for Android emulator? - Reddit
I am using Location from expo-location and it works in expo go on my iphone however when I run the app on the...
Read more >
How to use the expo-location.getCurrentPositionAsync ... - Snyk
getCurrentPositionAsync function in expo-location ... Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately....
Read more >
How to use getCurrentPositionAsync function in expo-location
if (status === 'granted') { const location = await Location. ... getCurrentPositionAsync(Showing top 4 results out of 315). Tabnine vs. GitHub Copilot.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found