Having trouble placing multiple Map markers from firebase (Realtime Database) in Flutter on Google Maps

Viewed 92

I am trying to get multiple "latitude" and "longitude" coordinates from firebase and place them on google maps keeping in mind that the coordinates may constantly change. However I haven't been able to do so. Below is my code showing how far ı have come.

Getting coordinates from firebase:

Future getAllDriversLocation() async {
    databaseReference
        .child("users")
        .child("Driver Coordinates")
        .onValue
        .listen(
      (event) {
        setState(
          () {
            var value = event.snapshot.value;
            driversLocation = Map.from(value)
                .values
                .map((e) => LocationCoordinates.fromJson(Map.from(e)))
                .toSet();

            _markers = driversLocation.map(
              (location) {
                print("I'm in Markers!");
                // final int index = driversLocation.indexOf(location);
                final int index = driversLocation.elementAt(location);
                final MarkerId markerId = MarkerId("Marker $index");

                return Marker(
                  markerId: markerId,
                  position: LatLng(location.latitude, location.longitude),
                );
              },
            );
            print(driversLocation);
          },
        );
      },
    );
    return driversLocation;
  }

My class:

class LocationCoordinates {
  final double latitude;
  final double longitude;
  final String name;
  LocationCoordinates({
    this.latitude,
    this.longitude,
    this.name,
  });
  factory LocationCoordinates.fromJson(Map<dynamic, dynamic> json) {
    return LocationCoordinates(
        latitude: json["latitude"],
        longitude: json["latitude"],
        name: json["DriverUID"]);
  }
}
0 Answers
Related