1

I use the "google_maps_flutter 2.1.8" package for a new project, but I can't get the 'onTap' function working. Not even to print something. After I spent the whole day trying to find the problem, I guess I need some help now. Do I have to activate the function somehow. I am grateful for any advice! My current code looks like this:


import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_controller/google_maps_controller.dart';
import 'package:test/SecondScreen.dart';


class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  late GoogleMapController mapController; //controller for Google map
  final Set<Marker> markers = new Set(); //markers for google map
  static const LatLng showLocation = const LatLng(51.2333, 6.783333); //initial Location
  late BuildContext _myContext; //Try OnTap Function Delete if not working


  @override
  Widget build(BuildContext context) {
    _myContext = context; //Try OnTap Function Delete if not working

    return  Scaffold(
      appBar: AppBar(
        title: Text("Multiple Markers in Google Map"),
        backgroundColor: Color.fromARGB(200, 13, 127, 229)
      ),
      body: GoogleMap( //Map widget from google_maps_flutter package
        zoomGesturesEnabled: true, //enable Zoom in, out on map
        initialCameraPosition: CameraPosition( //inital position in map
          target: showLocation, //initial position
          zoom: 15.0, //initial zoom level
        ),
        markers: getmarkers(), //markers to show on map
        mapType: MapType.normal, //map type
        onMapCreated: (controller) { //method called when map is created
          setState(() {
            mapController = controller;
          });
        },
      ),
    );
  }

  //Markers Only

  Set<Marker> getmarkers() { //markers to place on map
    setState(() {

      markers.add(Marker( //add first marker
        markerId: MarkerId(showLocation.toString()),
        position: showLocation, //position of marker
        infoWindow: InfoWindow( //popup info
          title: 'Marker Title First ',
          snippet: 'My Custom Subtitle',
          onTap: () {
            Navigator.push(
                _myContext,
                MaterialPageRoute(builder: (context) => SecondScreen())
            );
          },
        ),
        icon: BitmapDescriptor.defaultMarker, //Icon for Marker
      ));

      markers.add(Marker( //add second marker
        markerId: MarkerId(showLocation.toString()),
        position: LatLng(51.23, 6.78), //position of marker
        infoWindow: InfoWindow( //popup info
          title: 'Marker Title Second ',
          snippet: 'My Custom Subtitle',
          onTap: (){
            print('tap');
          },
        ),
        icon: BitmapDescriptor.defaultMarker, //Icon for Marker
      ));

      markers.add(Marker( //add third marker
        markerId: MarkerId(showLocation.toString()),
        position: LatLng(51.22, 6.776), //position of marker
        infoWindow: InfoWindow( //popup info
          title: 'Marker Title Third ',
          snippet: 'My Custom Subtitle',
        ),
        icon: BitmapDescriptor.defaultMarker, //Icon for Marker
      ));

      //add more markers here
    });

    return markers;
  }
}

We found the problem, I gave multiple markers the same MarkerId. After I changed it, it worked fine. Sorry guys, case closed!

0 Answers0