VladislavAntonyuk/MauiSamples

MauiMaps Android removed pin still appears on map

davefxy opened this issue · 8 comments

When I attempt to remove pin on MauiMaps Android, removed pin still appears on map.
I noticed in Android CustomMapHandler Markers is never is set to anything.
I added the following lines to AddPins() after the check for null at start of methos:
if (Markers == null) Markers = new List<Marker>();
Then the pin gets removed from map as expected.

Another issue I see and dont understand is the Action finished method for ImageSource.LoadImage appears to be called 4 times for each ImageSource.LoadImage () request. I examined Markers after adding a pin and there are 4 new Marker entries for an added pin.
I am using the latest version on the repository and Microsoft Visual Studio Community 2022 (64-bit) - Preview
Version 17.6.0 Preview 2.0

AddPins is only called in MapPins. in MapPins I clear all markers:

Why do you need to set it to null?

LoadImage is called and the app continues running. Then in the background image is loading and when ready the Action callback is executed. The marker may appear in a few seconds after you added Pin, depending on the image source (e.g. load image from the internet with low speed)

I think I determined what was happening and made the following changes to Android CustomMapHandler. Just rely on the Google map. The Markers List is not needed.
Remove Markers list
In MapPins() : remove mapHandler.Markers.Clear();
In AddPins() : add Map.Clear(); before foreach (var pin in mapPins)
In AddMarker(): remove marker from parm list, remove markers?.Add(marker);

This still needs work because I believe the Map.Clear() will possibly also eliminate polylines and other map elements.

Map.Clear() removes all objects from the map, not only Pins. If such changes work for your application, feel free to use them. Markers are equivalent to Pins. Changes of Markers list reflects changes on Map.

Using the Map.Clear() also prevents the GoogleMap markers from accumulating when adding/deleting pins since these are never cleared otherwise.

Why do you say they never cleaned if they actually do? On sample app click Add and Remove multiple times to see that pins disappear.
Once again markers collection represents pins collection. If you remove pin you remove marker. If you remove marker it will be remove from map.

Maybe we are running different versions of Maui. I use Microsoft Visual Studio Community 2022 (64-bit) - Preview Version 17.6.0 Preview 2.0.
I created a new app.: MauiMaps. I copied your code to this new app. I added the same nugets as your sample.
I run this Android app. I Added 2 new pins. I now have 4 pins displayed on map. I Removed 2 pins. There are still 4 pins displayed. Whenever a pin is added, it is added to Markers and Map.AddMarker. When I remove a pin, mapHandler.Markers.Clear(); is called. This clears the local Marker collection, and then adds the map.pins. However what clears the GoogleMap pins?
I see the same result when I compile and run your MauiMaps sample as is.
I do not see this problem if I eliminate the use of the Markers list and simply add to the GoogleMap and clear the GoogleMap.

Ok, I will double check it tomorrow

You are right.
Replace handler.Markers.Clear with

foreach (var marker in mapHandler.Markers)
{
marker.Remove();
}

Updated sample