Barney Chambers
Barney Chambers

Reputation: 2783

Xamarin - Android.GoogleMap remove marker

I am using the Android.Gms.Maps.GoogleMap package in Xamarin forms, and I can't seem to find a function to remove an instance of a Marker. I have saved a reference to all of my Marker in a List<Marker> and need to delete them periodically to replace them with new Marker

Here is my code:

var markerOptions = new MarkerOptions();
markerOptions.Draggable(false);
markerOptions.SetTitle(userLocations[i].user_id);
markerOptions.SetPosition(new LatLng(Convert.ToDouble(latLongs.latitude), Convert.ToDouble(latLongs.longitude)));
markerOptions.SetSnippet("Latitude: " + Convert.ToDouble(latLongs.latitude) + " Longitude: " + Convert.ToDouble(latLongs.longitude));
markerOptionsList.Add(markerOptions); //List<Marker>
map.AddMarker(markerOptions);

Upvotes: 0

Views: 1363

Answers (1)

Barney Chambers
Barney Chambers

Reputation: 2783

map.AddMarker() returns a Marker object. You must store this in a list to reference when you want to remove a Marker. The remove function is just marker.Remove();

Create list of markers:

List<Marker> marketList = new List<Marker>;

Add markers:

var markerOptions = new MarkerOptions();
markerOptions.Draggable(false);
markerOptions.SetTitle(userLocations[i].user_id);
markerOptions.SetPosition(new LatLng(Convert.ToDouble(latLongs.latitude), Convert.ToDouble(latLongs.longitude)));
markerOptions.SetSnippet("Latitude: " + Convert.ToDouble(latLongs.latitude) + " Longitude: " + Convert.ToDouble(latLongs.longitude));
Marker M = map.AddMarker(markerOptions);
markerList.Add(M);

To remove markers:

 foreach (Marker m in markerList)
 {
     m.Remove();
 }

Upvotes: 3

Related Questions