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.

Keep requesting location even when not in foreground

See original GitHub issue

Describe the bug
When <MapboxGL.UserLocation/> is used app keeps requesting GPS position from location manager and device. This works as intendend. However app keeps requesting position even when not in foreground. This can happen when user press home button or user switch to another app. App keeps requesting location until is killed by system or manually by user.

To Reproduce
clone repo

cd example
yarn
npx react-native run-android --variant=release
  • Run app on device.
  • Allow app to use location
  • Navigate to User location > Set User Location Render Mode or User Location Updates
  • Verify that you can see you position on the map
  • Verify that you can see GPS icon in the status bar. Not all the phones has it and not all the phone can turn it on. I understand that it might be difficult to reprocuce this issue without this icon.
  • Press home button
  • Verify that you can still see GPS icon in the status bar. It should not be there.
  • Open task manager and kill Example app
  • Verify that GPS icon disapper from status bar

Expected behavior
App should not request GPS postion when paused/in background/switched away. GPS icon in status bar should be visible only when app is in foreground.

Actual behavior
Remove location listeners when app leave foreground.

Screenshots
Note the small GPS icon in both screenshots.

Versions:

Additional context
This issue is quite a big deal as it do not comply with Google Policy. Apps cannot access location while app is not in background unless background permissions and special declariation is made. Apps that do not comply with this are taken down from Google Play Store. More info in the discussion. https://github.com/react-native-mapbox-gl/maps/discussions/1362

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:18 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
RobertSasakcommented, May 30, 2021

When tesing on actual device make sure that you go to Settings > Apps >Example app > Power managment and disable all the fancy power optimalisation. In my case I can see that app is killed within 10s after is in background.

I tried the same concept as in PR https://github.com/react-native-mapbox-gl/maps/pull/1365 directly in my app and it worked. I did not try the PR itself.

Profiling

I played a bit with profiling in Android Studio. It is very unstable and the app crashed a lot. But in the end I managed to get a following screenshot. On a real device it crashed even more and sometimes to not capture location request at all.

On the screenshot you can see power tab of profiling with CPU, network and location. I started the app navigated to location example, pressed the home butoon and resumed app. “The valley” in the middle is when app was in the background. Those small vertical lines precisely one second apart are actual location updates.

First row is the mapbox telemetry service. It create a location request but do not really ask for any location. Third and fifth row is the location manager. You can see as it is pause when app was in the background and started again on 5th row when app came back to foreground.

On the forth row you can see setFollowUserLocation. It registered for updates and keep registered also while the app is in the background.

As this test was performed on Android Emulator with API 29, I think system simply do not send location update while app is in the background. That would explain where there is a gap of vertical lines on line 4.

1reaction
RobertSasakcommented, May 26, 2021

I managed to make it work. And is seems like that GPS is not used when app leaves foreground when followUserLocation is set to false. Below code works.

<MapboxGL.MapView style={style}>
    <MapboxGL.Camera
        followUserMode={'normal'}
        followUserLocation={false}
    />
    <MapboxGL.UserLocation
        visible={true}
        renderMode="normal"
        showsUserHeadingIndicator
    />
</MapboxGL.MapView>

GPS is used for two different purposes. One is to draw the blue dot on the map and another to follow users location with camera. Those are two different things. First works fine and register and unregister it’s listeners when app enter foreground and leaves it. Second one does not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Requesting access to location in the background
Access to location is considered in the foreground if it happens while the app is open and visible to the user. If the...
Read more >
Choosing the Location Services Authorization to Request
The user may prefer to allow Always authorization so your app can record locations even when it's not in use, and without prompting...
Read more >
Request location permissions - Android Developers
... Handle different watch shapes · Create lists · Navigation · Exit full screen activities on Wear · Show confirmations · Keep your...
Read more >
How to Reliably Fetch Location in Background on Newest ...
You must first request permission for the foreground location, and then, if really necessary, you can request the background location.
Read more >
Request Location Permission Correctly in Android 11. - Medium
Best practices for requesting background location in Android. Android Location in ... Foreground and background apps used the same resource.
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