addMarker method

Future<void> addMarker(
  1. BuildContext context,
  2. PlacesModel poi, {
  3. bool removeAll = true,
  4. bool isFromFav = false,
})

addMarker method adds a marker to the map. It takes a BuildContext object, a PlacesModel object, and optional parameters removeAll and isFromFav. It adds a marker to the map with the given poi object and sets the marker's info window to display the name and description of the place. If removeAll is true, it removes all existing markers from the map before adding the new marker. If isFromFav is true, it adds the marker to the custom tour main markers set and removes the place from the displayed places list and adds it to the tour places list.

Implementation

Future<void> addMarker(BuildContext context, PlacesModel poi,
    {bool removeAll = true, bool isFromFav = false}) async {
  if (removeAll) {
    _markers.clear();
  }

  final String markerId = 'marker_${poi.id}.${poi.name}';

  DisplayedListProvider dlp =
      Provider.of<DisplayedListProvider>(context, listen: false);

  final Marker marker = Marker(
    draggable: true,
    markerId: MarkerId(markerId),
    position: LatLng(poi.latitude, poi.longitude),
    consumeTapEvents: true,
    infoWindow: InfoWindow(
      title: poi.name,
      snippet: poi.description,
      onTap: () {
        pinPillPosition = 10;
        currentlySelectedPin = poi;
      },
    ),
    icon: _iconMarker ?? BitmapDescriptor.defaultMarker,
    onTap: () {
      if (isFromFav) {
        showDialog(
            context: context,
            builder: (context) => Consumer2<FontsProvider, ColorProvider>(
                  builder: (BuildContext context, FontsProvider value,
                      ColorProvider value2, Widget? child) {
                    return AlertDialog(
                      backgroundColor: value2.colors.innerBackground,
                      title: Text('Remove from favorites?',
                          style: TextStyle(
                            color: value.fonts.primaryFontColor,
                            fontFamily: fontType,
                            fontSize: value.fonts.textSize,
                            fontWeight: FontWeight.bold,
                          )),
                      content: Text(
                        'Do you want to remove ${poi.name} from tour?',
                        style: TextStyle(
                          color: value.fonts.primaryFontColor,
                          fontFamily: fontType,
                          fontSize: value.fonts.textSize,
                        ),
                      ),
                      actions: [
                        TextButton(
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          child: Text('Cancel',
                              style: TextStyle(
                                color: LgAppColors.lgColor2,
                                fontFamily: fontType,
                                fontSize: value.fonts.textSize,
                              )),
                        ),
                        TextButton(
                          onPressed: () {
                            Navigator.pop(context);
                            removeMarker(markerId);

                            dlp.addDisplayedPlace(poi);
                            dlp.removeTourPlace(poi);
                          },
                          child: Text('Remove',
                              style: TextStyle(
                                color: value.fonts.primaryFontColor,
                                fontFamily: fontType,
                                fontSize: value.fonts.textSize,
                              )),
                        ),
                      ],
                    );
                  },
                ));
      } else {
        pinPillPosition = 10;
        currentlySelectedPin = poi;
      }
    },
    onDragEnd: (LatLng newPosition) {},
  );

  _markers.add(marker);
  //added this new if condition:
  if (isFromFav) {
    _customTourMainMarkers.add(marker);
    dlp.removeDisplayedPlace(poi);
    dlp.addTourPlace(poi);
  }

  notifyListeners();
}