Alex
Alex

Reputation: 827

How to add markers and set custom tap events on markers with Google Maps Plugin?

I have a list of markers like this

List<Marker> markers = <Marker>[
    new Marker("1", "Name 1", 28.421364, 77.333804,
        color: Colors.amber),
    new Marker("2", "Name 2", 28.418684, 77.340417,
        color: Colors.purple),
  ];

And the basic code to show and draw the map

class  HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
  Completer<GoogleMapController> _controller = Completer();
  static final CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(11.052992, 106.681612),
    zoom: 11,
  );
  @override
  Widget build(BuildContext context){
    return new Scaffold(
      body: GoogleMap(
        initialCameraPosition: _kGooglePlex,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),
    );
  }
}

How can I add markers from my List<Marker> to Google Maps and add onTap method to those markers?

Upvotes: 1

Views: 1012

Answers (1)

mirkancal
mirkancal

Reputation: 5345

As with google_maps_flutter 0.3.0, marker api has changed. Before 0.3.0, you have to add your markers on controller like that.

mapController.addMarker(
  MarkerOptions(
    position: LatLng(37.4219999, -122.0862462),
  ),
);

After the change on marker api,GoogleMap widget has a property named markers which takes Set of markers, not list of Markers. So you can give a set of markers to your GoogleMap widget. Also markers have onTap property for tapping.

Upvotes: 1

Related Questions