Flutter/Dart Add custom Tap Events for Google Maps Marker

dartfluttergoogle maps

How would I add a custom handler for tap events for Google Maps Marker (google_maps_flutter)? I can only see consumeTapEvents which doesn't actually take in any functions but only bool. I've thought of using GestureDetector but doesn't seem quite right.

What is the standard way of handling events on Google Map's Markers? I'm trying to navigate to a new page upon click.

Thanks

Best Answer

With the release of version ^0.3.0+1 a new Marker API was introduced which handles markers as widgets (including an onTap() method). This way a Google Map has a markers: option which admits a list of Marker objects. Each of the elements can be defined like this:

Marker(
  markerId: MarkerId("id"), // a string for marker unique id
  icon: BitmapDescriptor.defaultMarker(), // options for hues and custom imgs
  position: LatLng(lat, long), // lat and long doubles

  onTap: () {
    //this is what you're looking for!
  }

),

Much easier than the former controller approach!

Related Topic