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
102 stars 93 forks source link

Unable to hide/show CurrentLocationLayer #109

Closed hrhernes closed 7 months ago

hrhernes commented 7 months ago

Describe the bug I need to hide and show the CurrentLocationLayer. When the map loads CurrentLocationLayer is shown. When a button is clicked the layer visibility should be toggled on and off. Currently when the CurrentLocationLayer gets toggled off for the first time it is not possible to show it again (it does not appear anymore). See the example code below.

To Reproduce

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

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

  @override
  State<MinimumExample> createState() => _MinimumExampleState();
}

class _MinimumExampleState extends State<MinimumExample> {
  bool _showCurrentLocation = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Minimum Example'),
      ),
      body: FlutterMap(
        options: const MapOptions(
          initialCenter: LatLng(0, 0),
          initialZoom: 1,
          minZoom: 0,
          maxZoom: 19,
        ),
        children: [
          TileLayer(
            urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
            userAgentPackageName:
                'net.tlserver6y.flutter_map_location_marker.example',
            maxZoom: 19,
          ),
          if (_showCurrentLocation) CurrentLocationLayer(),
          Align(
            alignment: Alignment.bottomRight,
            child: Padding(
              padding: const EdgeInsets.all(20.0),
              child: FloatingActionButton(
                onPressed: () {
                  setState(() {
                    _showCurrentLocation = !_showCurrentLocation;
                  });
                  print('Show location: $_showCurrentLocation');
                },
                child: Icon(
                  _showCurrentLocation ? Icons.location_disabled : Icons.my_location,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Expected behavior The CurrentLocationLayer visibility should be toggled on and off.

Smartphone (please complete the following information):

Additional context Maybe I should try to show and hide the layer in some other way, I am not sure.

tlserver commented 7 months ago

fixed in 8.0.8