Skip to content

[Bug]: UserLocation stops updating after restarting app with background foregroundService on Android #3965

@Slo1k

Description

@Slo1k

Mapbox Implementation

Mapbox GL

Mapbox Version

default

React Native Version

0.78.2

Platform

Android

@rnmapbox/maps version

10.1.42

Standalone component to reproduce

import React, {useState} from "react";
import MapboxGL, {Camera, MapView, UserLocation} from "@rnmapbox/maps";
import {ActivityIndicator, Platform, View} from "react-native";

Platform.OS === "android" && MapboxGL.requestAndroidLocationPermissions();

export const START_ZOOM_LEVEL = 15;
export const MAX_ZOOM_LEVEL = 18;
export const MIN_ZOOM_LEVEL = 1;

const DEFAULT_LOCATION = {
  latitude: 52.52,
  longitude: 13.405,
};

export default function AppMap() {
  const [mapLoaded, setMapLoaded] = useState(false);

  return (
    <View style={{flex: 1}}>
      {!mapLoaded && (
        <View
          style={{
            position: "absolute",
            zIndex: 200,
            bottom: "50%",
            alignSelf: "center",
          }}>
          <View
            style={{
              backgroundColor: "white",
              padding: 8,
              borderRadius: 9999,
            }}>
            <ActivityIndicator size={48} />
          </View>
        </View>
      )}

      <MapView
        logoEnabled={true}
        attributionEnabled={true}
        rotateEnabled={false}
        zoomEnabled
        scrollEnabled
        pitchEnabled={false}
        scaleBarEnabled={false}
        onDidFinishLoadingMap={() => {
          setMapLoaded(true);
        }}
        style={{flex: 1}}>
        <>
          <Camera
            minZoomLevel={MIN_ZOOM_LEVEL}
            maxZoomLevel={MAX_ZOOM_LEVEL}
            allowUpdates={true}
            animationMode="none"
            defaultSettings={{
              centerCoordinate: [
                DEFAULT_LOCATION.longitude,
                DEFAULT_LOCATION.latitude,
              ],
              zoomLevel: START_ZOOM_LEVEL,
            }}
          />

          <UserLocation
            androidRenderMode="normal"
            requestsAlwaysUse
            minDisplacement={10}
            visible={true}
            onUpdate={res => {
              console.log("User location updated:", res);
            }}
          />
        </>
      </MapView>
    </View>
  );
}

Observed behavior and steps to reproduce

Steps to reproduce

  1. Open the app and display the map with UserLocation.
  2. Start some kind of a android foregroundService that works even when the app is closed
  3. Restart the app.
  4. Observe that the user location updates only a couple of times and then stops.
  5. Switch to the deprecated renderMode={UserLocationRenderMode.Native} and notice that the location updates correctly, but onUpdate remains non-functional.

Expected behavior

The map should continuously update the user’s location while the foreground service is running, even after the app is restarted.

Notes / preliminary analysis

When I start a foregroundService with android:foregroundServiceType="location", my location on the map stops updating after restarting the app. The position is retrieved at most twice and then stops completely.

If I use the deprecated renderMode={UserLocationRenderMode.Native} in UserLocation, the location on the map updates correctly, but the onUpdate callback still doesn’t return anything.

It looks like the issue is related to the interaction between Mapbox and Android foreground services. From what I’ve tested, the specific type of foregroundService doesn’t seem to make a difference.

Additional links and references

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🪲Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions