How to add tap event to google map in flutter

In this Google Flutter code snippet example, we are going to learn How to add tap event to google map in flutter

You can copy and adopt this source code example to your flutter project easily without issues.

add this code to your pubspec.yaml file

google_maps_flutter: ^0.5.19

Create a tap_events_google_map.dart file (use any name of your choice) and add this code:

 import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class TapEventsOnMap extends StatefulWidget {
  @override
  _TapEventsOnMapState createState() => _TapEventsOnMapState();
}

class _TapEventsOnMapState extends State<TapEventsOnMap> {
  Completer<GoogleMapController> _controller = Completer();
  static GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  static const LatLng _center = const LatLng(45.521563, -122.677433);
  static const LatLng _anotherLatLng = const LatLng(45.345563, -122.347433);

  void _onMapCreated(GoogleMapController controller) {
    _controller.complete(controller);
  }



  static MarkerId markerId1 = MarkerId("1");
  static MarkerId markerId2 = MarkerId("12");

  final Set<Marker> _markers = {
    Marker(
markerId: markerId1,
position: _center,
infoWindow: InfoWindow(
  title: 'Custom Marker',
          snippet: 'Inducesmile.com',
),
onTap: (){
  _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("Marker Tapped!!"),));
},
    ),
    Marker(
markerId: markerId2,
position: _anotherLatLng,
infoWindow: InfoWindow(
  title: 'Another Marker',
          snippet: 'Inducesmile.com',
),
onTap: (){
  _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("Marker Tapped!!"),));
},


    )
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
          title: Text('Tap Events Google Map'),
          backgroundColor: Colors.red,
        ),
        body: GoogleMap(
          onTap: (latLng){
  _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text("Latitude: ${latLng.latitude}, Longitude: ${latLng.longitude}"),));
},
          markers: _markers,
          onMapCreated: _onMapCreated,
          initialCameraPosition: CameraPosition(
            target: _center,
            zoom: 11.0,
          ),
        ),
      ),
    );
  }
}

In your main.dart file (main entry point of the app), add this:

import 'package:inducesmile/tap_events_google_map.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch: Colors.red),
      home: new TapEventsOnMap(),
    );
  }
}

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment