ploj
ploj

Reputation: 11

Including markers in google map for flutter based on Json file

I'm trying to add marker to my map widget based on the string in a Json file. However i have no idea how the marker thingy works after the .addMarker no longer applied. Can anyone please explain on it?

Upvotes: 1

Views: 816

Answers (1)

Karim Elghamry
Karim Elghamry

Reputation: 1421

The markers: attribute in the Flutter_Google_maps widget now takes a Set<Marker>().

to add markers, first you should create an empty Set of type Marker and assign it to the markers: attribute in the google maps widget. Consequently, you append the desired Marker() to the created set using the add() method. Finally, you use setState() to rebuild the widget and display the updated Markers set on the UI.

Example:

class MapsScreen extends StatefulWidget {
  @override
  _MapsScreenState createState() => _MapsScreenState();
}

class _MapsScreenState extends State<MapsScreen> {
  Completer<GoogleMapController> _mapsController = Completer();
  Set<Marker> _myMarkers = Set<Marker>();

  @override
  Widget build(BuildContext context) {
  return Scaffold(
  floatingActionButton: FloatingActionButton(
    onPressed: () {
      _myMakers.add(
      markerId: MarkerId("current"),
      position: LatLng(yourLatitude, yourLongitude),
      );
       setState((){});
      )},
    ),
  body: GoogleMap(
    initialCameraPosition: _anyCameraPosition,
    markers: _myMarkers,
    onMapCreated: (GoogleMapController controller) {
        _mapsController.complete(controller);
     },
    );
  );

 }
}

you have the option to use any state management pattern you prefer.

Upvotes: 1

Related Questions