2

I am using google_maps_flutter for creating a Google map with two types of markers. When one type of marker is dragged over the other, I want to remove the dragged marker and then perform a function. I am able to do this.

But, in case the dragged marker is dropped in a wrong position, then I want to place the marker back to its original position. I couldn't find any event or method to achieve this. How can this be done?

Here is my code for adding markers:

markers.add(
  Marker(
    markerId: packageMarkerId,
    draggable: true,
    icon: _unassignedOrderMarkerIcon,
    onTap: () {
      print('marker tapped');
    },
    position: LatLng(coordinates.latitude, coordinates.longitude),
    onDragEnd: (LatLng latLng) {
      double newLat =
        double.parse(latLng.latitude.toStringAsFixed(1));
      double newLong =
        double.parse(latLng.longitude.toStringAsFixed(1));
      for (var user in usersDocuments) {
        Map<String, dynamic> userLocation =
          user.data['user_location'];
      if (userLocation != null) {
        if (newLat == userLocation['latitude'] &&
              newLong == userLocation['longitude']) {
           print('Equal coords: $latLng');
           markers.removeWhere((element) {
             print('Marker removed!');
             return element.markerId == packageMarkerId;
           });
           setState(() {
             _markers = markers;
           });
           return;
        } else {
           print('Unequal coords, not merged!');
           print(userLocation['latitude']);
           print(userLocation['longitude']);
           return;
         }
       }
      }
    },
  ),
);
Bhawna Saroha
  • 603
  • 1
  • 11
  • 28

0 Answers0