flutter-geolocator icon indicating copy to clipboard operation
flutter-geolocator copied to clipboard

[Question]: Geolocator.getPositionStream triggers frame refreshes when used with MapLibre_gl

Open tmorell opened this issue 5 months ago • 2 comments

Please check the following before submitting a new issue.

Please select for which platform(s) you need help

  • [x] Android
  • [ ] iOS
  • [ ] Linux
  • [ ] macOS
  • [ ] Web
  • [ ] Windows

Your question

Using Geolocator.getPositionStream with MapLibre_gl causes frame refreshes on Android, even when the GPS coordinates are not changing. This behavior, observed on Android but not iOS, leads to constant frame generation.

Versions:

  • geolocator: 14.0.1
  • maplibre_gl: 0.22.0

Sample code to reproduce the issue:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:maplibre_gl/maplibre_gl.dart';

class TestGeolocator extends StatefulWidget {
  const TestGeolocator({super.key});

  @override
  State<TestGeolocator> createState() => _TestGeolocatorState();
}

class _TestGeolocatorState extends State<TestGeolocator> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await _trackCoordinates();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: MapLibreMap(
          compassViewPosition: CompassViewPosition.topRight,
          styleString: 'https://style_url',
          myLocationEnabled: true,
          initialCameraPosition: const CameraPosition(
            target: LatLng(40.947970551634754, -4.118041773948952),
            zoom: 18.5,
          ),
          trackCameraPosition: true,
        ),
      ),
    );
  }

  Future<void> _trackCoordinates() async {
    var locationSettings = const LocationSettings(accuracy: LocationAccuracy.high, distanceFilter: 5);
    Geolocator.getPositionStream(locationSettings: locationSettings).listen((position) async {
      print('${position.latitude}, ${position.longitude}');
    });
  }
}
Image

Any idea on how to prevent this?

Thanks!

Version

14.0.1

tmorell avatar Jun 08 '25 23:06 tmorell