tlserver / flutter_map_location_marker

A flutter map plugin for displaying device current location.
https://pub.dev/packages/flutter_map_location_marker
BSD 3-Clause "New" or "Revised" License
108 stars 98 forks source link

Implementing a setState causes the Geolocator to stop. #106

Closed ElectricArdvark closed 9 months ago

ElectricArdvark commented 9 months ago

Describe the bug When ever there is a setState implemented in code either in the FlutterMap or a FAB the Geolocator seems to stop the start again and on every click this comes up in the debug console: E/FlutterGeolocator( 3840): Geolocator position updates stopped E/FlutterGeolocator( 3840): There is still another flutter engine connected, not stopping location service E/FlutterGeolocator( 3840): Geolocator position updates started

To Reproduce Running this code should get you the same issue

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:latlong2/latlong.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'app',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
            seedColor: const Color.fromARGB(255, 80, 174, 52)),
        useMaterial3: true,
      ),
      home: const MapWid(),
    ),
  );
}

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

  @override
  State<MapWid> createState() => _MapWidState();
}

class _MapWidState extends State<MapWid> with TickerProviderStateMixin {
  MapController mapController = MapController();

  bool _isPl1BusVisible = false;

  void _toggleMarkerVisibility() {
    setState(() {
      _isPl1BusVisible = !_isPl1BusVisible;
    });
  }

  Polyline route1plbus = Polyline(points: [
    const LatLng(51.46887229500683, -0.0948180853591833),
    const LatLng(51.56287780290259, -0.09550473084007167),
    const LatLng(51.46673355111825, -0.33583064915101585),
  ]);

  List<Widget> _buildPolylineLayers() {
    final List<Widget> layers = [];

    void addPolylineLayer(
        bool isVisible, List<LatLng> points, double strokeWidth, Color color,
        {bool isDotted = false}) {
      if (isVisible) {
        layers.add(
          PolylineLayer(
            polylines: [
              Polyline(
                points: points,
                strokeWidth: strokeWidth,
                color: color,
                isDotted: isDotted,
              ),
            ],
          ),
        );
      }
    }

    addPolylineLayer(_isPl1BusVisible, route1plbus.points, 4.0, Colors.blue);

    return layers;
  }

  Widget _buildMarker() => 
       Tooltip(
        message: "Bus 34B",
       child: Icon(
          Icons.directions_bus,
          color: Colors.blue,
          size: 30,
        ),
      );

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: Brightness.dark,
    ));
    return Scaffold(
      body: FlutterMap(
        mapController: mapController,
        options: MapOptions(
          initialCenter: const LatLng(51.46887229500683, -0.0948180853591833),
          initialZoom: 14.5,
          //minZoom: 11.5,
          maxZoom: 19.5,
          onTap: (_, __) {
            setState(() {
              _isPl1BusVisible = false;
            });
          },
        ),
        children: [
          TileLayer(
            urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
          ),
          CurrentLocationLayer(),
          ..._buildPolylineLayers(),
          MarkerLayer(markers: [
            Marker(
              width: 20.0,
              height: 20.0,
              point: const LatLng(51.46887229500683, -0.0948180853591833),
              child: GestureDetector(
                onTap: () {
                  _toggleMarkerVisibility();
                  setState(() {
                    _isPl1BusVisible = true;
                  });
                },
                child: const Tooltip(
                  message: "Bus 38A",
                  child: Icon(Icons.location_on,
                      color: Color.fromARGB(255, 205, 199, 18)),
                ),
              ),
            )
          ]),
        ],
      ),
    );
  }
}

Smartphone:

Additional context This all could be simply an issue on my m

ElectricArdvark commented 9 months ago

Fixed in 8.0.6

ElectricArdvark commented 9 months ago

Seems to happen also when changing Geolocator settings

CurrentLocationLayer(
     positionStream: const LocationMarkerDataStreamFactory()
           .fromGeolocatorPositionStream(
        stream: Geolocator.getPositionStream(
            locationSettings: AndroidSettings(
              intervalDuration:
                 Duration(milliseconds: 1000)),
    ),
  ),
),
tlserver commented 9 months ago

You may want to create the stream in initState() or widget constructor, otherwise a new stream is created for every widget build and the widget consider the stream is changed. I will check the example project to see if they need to be updated.