James
James

Reputation: 729

How do I reduce latency for Flutter Geolocator on iOS?

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!

Upvotes: 0

Views: 194

Answers (0)

Related Questions