0

I've built an app using Flutter that allows users to track how far they have travelled from a starting point, a bit like any map-based or sports tracking app. With the app, a high accuracy is important, as well as low latency. For this functionality, I am using the Geolocator package.

On Android, I haven't noticed much of a latency issue at all but for iOS, I have been told by those using the app on the platform that there's quite a bit of latency, which is quite frustrating. That's what I need to correct and I am hoping you can help me figure that out.

After initially getting the user's starting location with Geolocator.getCurrentPosition(), I then use a position stream to get the live location of the user, like so:

      StreamSubscription<Position> positionStream =
          Geolocator.getPositionStream(locationSettings: locationSettings)
              .listen((Position? position) async {
        if (position != null) {
          if (lengthUnit == true) {
            setState(() {
              liveLatitude = position.latitude.toStringAsFixed(6);
              liveLongitude = position.longitude.toStringAsFixed(6);
              distanceBetweenPoints = SphericalUtil.computeDistanceBetween(
                    LatLng(double.parse(initialLatitude),
                        double.parse(initialLongitude)),
                    LatLng(double.parse(liveLatitude),
                        double.parse(liveLongitude)),
                  ) *
                  3.28084;
            });
          } else {
            setState(() {
              liveLatitude = position.latitude.toStringAsFixed(6);
              liveLongitude = position.longitude.toStringAsFixed(6);
              distanceBetweenPoints = SphericalUtil.computeDistanceBetween(
                LatLng(double.parse(initialLatitude),
                    double.parse(initialLongitude)),
                LatLng(double.parse(liveLatitude), double.parse(liveLongitude)),
              );
            });
          }
        } else {
          setState(() {
            liveLatitude = initialLatitude;
            liveLongitude = initialLongitude;
          });
        }
      });

To try to reduce latency and improve the accuracy of the measurement, I have implemented some location settings, as follows:

      if (defaultTargetPlatform == TargetPlatform.android) {
        locationSettings = AndroidSettings(
            accuracy: LocationAccuracy.high,
            distanceFilter: 0,
            forceLocationManager: true,
            intervalDuration: const Duration(milliseconds: 5),
            foregroundNotificationConfig: const ForegroundNotificationConfig(
              notificationText:
                  "The app will continue to receive your location even when you aren't using it",
              notificationTitle: "Running in Background",
            ));
      } else if (defaultTargetPlatform == TargetPlatform.iOS) {
        locationSettings = AppleSettings(
          accuracy: LocationAccuracy.high,
          distanceFilter: 0,
          pauseLocationUpdatesAutomatically: true,
          showBackgroundLocationIndicator: false,
        );
      } else {
        locationSettings = const LocationSettings(
          accuracy: LocationAccuracy.high,
          distanceFilter: 0,
        );
      }

As you can see, I have been able to specify the interval duration on Android, which I'm presuming is why I haven't noticed any latency issues on the platform. However, I don't really know how to do that for iOS as there doesn't seem to be a similar setting, but I really do need to reduce latency.

Please could someone point me in the right direction? How can I modify these settings so that I can reduce latency on iOS? Or is there a better way of doing it altogether? Thank you!

4
  • Don't set pauseLocationUpdatesAutomatically: true it isn't helpful. iOS will deliver locations at a rate of about once per second. You can't get it any faster, but that should be more than adequate for most purposes. Commented May 11, 2024 at 7:42
  • Hey @Paulw11 thanks for that. So basically with me having set that, it could have been potentially causing an additional delay/latency? It's a shame you can't specify a rate faster than 1/second like you can on Android, but I'll try what you have said to see if that helps. Commented May 11, 2024 at 8:19
  • Yes. If location updates pause and you have not implemented the delegate method to be told they have been paused then you won't start the updates again Commented May 11, 2024 at 9:15
  • Got you mate. I'm fairly certain I haven't implemented that delegate method. I'll get on with the changes and I'll let you know what happens :) Commented May 11, 2024 at 12:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.