0

I'm creating simple app, and I need that when the map is tapped a marker should be added. I'm using the onTap property of the google_maps_flutter plugin:

var markers = Set<Marker>();
...

body: Stack(
  children: [
    Positioned.fill(
      child: GoogleMap(
        onTap: (LatLng point) {
        print(point.longitude.toString());
          final snackBar = SnackBar(
            content: Text(point.longitude.toString()),
            action: SnackBarAction(
              label: 'Undo',
              onPressed: () {
                // Blah.
              },
            ),
          );
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
          setState(() {
            markers.add(Marker(
              markerId: MarkerId(point.toString()),
              position: point,
              infoWindow: InfoWindow(
                title: 'I am a marker in ${point}',
              ),
              icon: BitmapDescriptor.defaultMarkerWithHue(
                  BitmapDescriptor.hueMagenta),
              anchor: Offset(100, 160),
            ));
          });
        },
        //--
        myLocationEnabled: true,
        zoomControlsEnabled: false,
        markers: markers,
        initialCameraPosition: CameraPosition(
          target: currentLocation,
        ),
        onMapCreated: (GoogleMapController controller) {
          _mapController.complete(controller);
        },
      ),
    ),

 ...

I added a print and a snackbar just to make sure I was getting the location information, and works just fine. I don't get any errors anywhere. I also added the map directly into the body (no Stack), and still doesn't do anything. I'm not sure what's happening. I can add more portions of then code if needed.

Zoe
  • 27,060
  • 21
  • 118
  • 148
cubanGuy
  • 1,226
  • 3
  • 26
  • 48
  • Did you check that the build method is invoked after setState and does it construct a new Google map widget with the extended marker list? That's what needs to happen. – Bernhard Aug 27 '21 at 20:53
  • What's the function that isn't working. The original one, not the snack bar. – Huthaifa Muayyad Aug 27 '21 at 21:02
  • @HuthaifaMuayyad, the snack bar works accordingly. – cubanGuy Aug 28 '21 at 14:58
  • I can't believe I made this silly mistake. The issue was the anchor. I just changed it to `anchor: const Offset(0.5, 0.5)`, and voilá! It works. Thanks all for the input. – cubanGuy Aug 28 '21 at 15:17

0 Answers0