Skip to content

[Bug]: In IOS is it not tracking location properly. #1531

@error404sushant

Description

@error404sushant

Required Reading

  • Confirmed

Plugin Version

flutter_background_geolocation: ^4.16.9

Flutter Doctor

sushantkumar@Sushants-MacBook-Pro citizen_mobile_app % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.27.2, on macOS 15.3.2 24D81 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2025.1.3)
[✓] VS Code (version 1.101.0)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

Mobile operating-system(s)

  • iOS
  • Android

Device Manufacturer(s) and Model(s)

iPhone 15

Device operating-systems(s)

iOS 18.5

What happened?

On iOS, location tracking is not working properly. After covering some distance, it only shows around 10 to 20 points, and then it stops showing any new location data—no latitude or longitude. It just draws a straight line to the last known location. For context, the app was open and running in the background during this issue.

Image Image

Plugin Code and/or Config

Future<void> init() async {
    // Initialize app auth provider
    // AppAuthProvider appAuthProvider =
    //     Provider.of<AppAuthProvider>(Get.context!, listen: false);

    // if(userId == null){
    //   await HiveRegisterAndOpenBox.registerAndOPenBox();
    // }
    await HiveRegisterAndOpenBox.registerAndOPenBox();
    //Logged in user info from local db
    User user = AuthStorageService().getUserDetailDBomDb();
    //Device params
    Map<String, dynamic> deviceParams = await bg.Config.deviceParams;
    // Fetch a JSON Web Token from dev server.
    bg.TransistorAuthorizationToken token =
        await bg.TransistorAuthorizationToken.findOrCreate(
            'binimise',
            "${user.name.toString()}-${user.id.toString()}",
            AppConstants.locationUpdateUrl);

    debugPrint("Device params: $deviceParams and User id is ${user.id}");
    String? deviceFcmToken = await AppPushNotificationService.getToken();
    //Save device fcm toke
    logLocationEvent(
        event: 'Device fcm', details: '${deviceFcmToken}', isSuccess: true);

    // Log initialization
    logLocationEvent(
        event: 'Initialization',
        details: 'Location tracking service initialized for user: ${user.name}',
        isSuccess: true);

    //On geofence entry and exit
    bg.BackgroundGeolocation.onGeofence((GeofenceEvent event) async {
      //Exit
      if (event.action == "EXIT") {
        AppPushNotificationAction().geofenceEntryAndExitNotification(
            isExit: true, identifier: event.identifier);
        //Geofence exit
        logLocationEvent(
            event: 'Geofence Exit',
            details: 'Exited geofence: ${event.identifier}',
            coordinates: {
              'latitude': event.location.coords.latitude,
              'longitude': event.location.coords.longitude
            },
            isSuccess: true);
      } else {
        AppPushNotificationAction().geofenceEntryAndExitNotification(
            isExit: false, identifier: event.identifier);
        //Geofence entry
        logLocationEvent(
            event: 'Geofence Entry',
            details: 'Entered geofence: ${event.identifier}',
            coordinates: {
              'latitude': event.location.coords.latitude,
              'longitude': event.location.coords.longitude
            },
            isSuccess: true);
      }
    });

    // Log location updates
    // bg.BackgroundGeolocation.onLocation((Location location) {
    //   logLocationEvent(
    //       event: 'Location Update',
    //       details: 'Location updated',
    //       coordinates: {
    //         'latitude': location.coords.latitude,
    //         'longitude': location.coords.longitude,
    //         'accuracy': location.coords.accuracy,
    //         'speed': location.coords.speed
    //       },
    //       isSuccess: true);
    // });

    //On enabled change
    BackgroundGeolocation.onEnabledChange((value) {
      logLocationEvent(
          event: 'On change enable',
          details: 'Location tracking enabled: $value',
          isSuccess: true);
    });

    // Configure the plugin
    try {
      await bg.BackgroundGeolocation.ready(bg.Config(
        transistorAuthorizationToken: token,
        url: '${AppConstants.locationUpdateUrl}/locations',
        params: deviceParams,
        desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
        distanceFilter: 10.0,
        stopOnTerminate: false,
        locationUpdateInterval: 2000,
        disableElasticity: true,
        autoSync: true,
        preventSuspend: true,
        startOnBoot: true,
        debug: true,
        enableHeadless: true,
        geofenceModeHighAccuracy: true,
        activityRecognitionInterval: 10000,
        geofenceInitialTriggerEntry: false,
        activityType: bg.Config.ACTIVITY_TYPE_FITNESS,
        pausesLocationUpdatesAutomatically: false, // iOS
        logLevel: bg.Config.LOG_LEVEL_VERBOSE,
          showsBackgroundLocationIndicator: true,

      )).then((bg.State state)async {
        if (!state.enabled) {
          await bg.BackgroundGeolocation.start();
          debugPrint("State enabled ${state}");
        }
        logLocationEvent(
            isSuccess: true,
            details: "Geofence is ready",
            event: "Geofence ready");
      });
    } catch (e) {
      debugPrint("Error in location tracking: $e");
      logLocationEvent(
          isSuccess: false,
          details: "Error in location while getting ready: $e",
          event: "Geofence ready");
    }
  }

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions