Reputation: 83
I have added Google Maps for Flutter i know how to add a marker as it is given clearly in there examples
MarkerOptions _options = new MarkerOptions(
position: LatLng(
driver_lat,
driver_lng,
),
infoWindowText:
const InfoWindowText('An interesting location', '*'));
Marker marker = new Marker('1', _options);
//Adding Marker
googleMapController.addMarker(_options);
And i am removing the marker like below
googleMapController.removeMarker(marker);
for adding the marker it is taking MarkerOptions object as a parameter but for removing the marker it is asking for Marker object as parameter and my removing marker code is not working. i am getting the below error
Failed assertion: line 201 pos 12: '_markers[marker._id] == marker': is not true.
Upvotes: 3
Views: 13141
Reputation: 683
If anyone still struggling with removing a specific marker try this method;
MarkerId id = MarkerId("Pickup");
//markers[id] = {} as Marker; clear all markers
markers.removeWhere((key, value) => key == id); //clear a specific marker
Upvotes: 0
Reputation: 1207
2020 answer:
.clearMarkers() has been deprecated as now each Marker is a Widget stored in a map. The correct way to clear all of the markers now on your Google Map is to set the state of of your marker Map to an empty map.
e.g.
...
onPressed: () {
setState(() {
gMapMarkers = {};
});
}
....
Upvotes: 3
Reputation: 1669
There are two ways to do this, one is via clearMarkers()
Method
mapController.clearMarkers();
Another one is via targeting each marker returned by mapController.markers
mapController.markers.forEach((marker){
mapController.removeMarker(marker);
});
Upvotes: 3
Reputation: 10175
I've came across this issue myself with the google_maps_library
and the main cause of this issue '_markers[marker._id] == marker': is not true.
is the fact that all GoogleMapsController
methods return a Future
, so this error is, let's say a concurrency issue since the method cals are async
.
The correct way to add/remove a marker would be:
_testRemoveMarker() async {
Marker marker = await _mapController.addMarker(...markerOption..);
_mapController.removeMarker(marker);
}
_clearMarkersAndRead() async {
_mapController.clearMarkers().then((_) {
//TODO: add makrers as you whish;
});
}
So, if you do any operations with the markers add/remove/update, you should be sure that the previous operation that involved markers is completed.
Upvotes: 0
Reputation: 1039
Use clearMarkers()
. It will clear all markers in your map. So try googleMapController.clearMarkers();
Upvotes: 2