Want to add marker to Google Maps with new Marker API in Flutter? Use the below flutter example code to add marker to Google Maps with new Marker API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | Map<MarkerId, Marker> markers = <MarkerId, Marker>{}; // CLASS MEMBER, MAP OF MARKS void _add() { var markerIdVal = MyWayToGenerateId(); final MarkerId markerId = MarkerId(markerIdVal); // creating a new MARKER final Marker marker = Marker( markerId: markerId, position: LatLng( center.latitude + sin(_markerIdCounter * pi / 6.0) / 20.0, center.longitude + cos(_markerIdCounter * pi / 6.0) / 20.0, ), infoWindow: InfoWindow(title: markerIdVal, snippet: '*'), onTap: () { _onMarkerTapped(markerId); }, ); setState(() { // adding a new marker to map markers[markerId] = marker; }); } GoogleMap( onMapCreated: _onMapCreated, initialCameraPosition: const CameraPosition( target: LatLng(-33.852, 151.211), zoom: 11.0, ), // TODO(iskakaushik): Remove this when collection literals makes it to stable. // https://github.com/flutter/flutter/issues/28312 // ignore: prefer_collection_literals markers: Set<Marker>.of(markers.values), // YOUR MARKS IN MAP ) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.