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

How to stop showing location marker #11

Closed mtan11 closed 2 years ago

mtan11 commented 2 years ago

I want to create a button toggle get location user. How to stop showing location marker when i tap on the toggle button. Thanks for your help.

jiazeh commented 2 years ago

The switch display marker is as follows?

bool _isShowLocMarker = false;

FlutterMap(
  options: MapOptions(
    ...
  ),
  children: [
    TileLayerWidget(
      options: TileLayerOptions(
        urlTemplate:
            'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
        subdomains: ['a', 'b', 'c'],
        maxZoom: 19,
      ),
    ),
    if (_isShowLocMarker)
      LocationMarkerLayerWidget(
        plugin: LocationMarkerPlugin(
          centerCurrentLocationStream:
              _centerCurrentLocationStreamController.stream,
          centerOnLocationUpdate: _centerOnLocationUpdate,
        ),
      ),
      Positioned(
        right: 20,
        bottom: 80,
        child: Column(
          children: [
            FloatingActionButton(
              heroTag: null,
              onPressed: () => setState(() {
                _isShowLocMarker = false;
                _centerCurrentLocationStreamController.close();
              }),
              child: Icon(
                Icons.gps_off,
                color: Colors.white,
              ),
            ),
            SizedBox(height: 20),
            FloatingActionButton(
              heroTag: null,
              onPressed: () {
                // Automatically center the location marker on the map when location updated until user interact with the map.
                setState(() {
                  if (!_isShowLocMarker) {
                    _centerCurrentLocationStreamController =
                        StreamController<double>();
                  }
                  _centerOnLocationUpdate =
                      CenterOnLocationUpdate.first;
                });
                // Center the location marker on the map and zoom the map to level 18.
                _centerCurrentLocationStreamController.add(18);
                _isShowLocMarker = true;
              },
              child: Icon(
                Icons.my_location,
                color: Colors.white,
              ),
            ),
          ],
        )),
   ]
),