dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.98k stars 1.72k forks source link

[Maps] [Regression from Xamarin.Forms.Maps] Changing Location on a Pin does nothing #12916

Open mfeingol opened 1 year ago

mfeingol commented 1 year ago

Description

Changing the Location property on a Microsoft.Maui.Controls.Maps.Pin does not change the location of the Pin on the map.

Steps to Reproduce

  1. Create a map.
  2. Add a pin at a location.
  3. Change the location of the pin: pin. Location = something;
  4. Observe that the pin does not change its location on the map.

Link to public reproduction project repository

Shouldn't be needed

Version with bug

7.0 (current)

Last version that worked well

7.0 (current)

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

Removing the pin and re-adding it works. This is annoying and code that updates a pin's location needs to be changed everywhere when porting from Xamarin.Forms.Maps

Relevant log output

No response

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

mfeingol commented 9 months ago

This is still an issue in .NET 8

mfeingol commented 6 months ago

I believe the reason this doesn't work is this line:

https://github.com/dotnet/maui/blob/main/src/Core/maps/src/Handlers/MapPin/MapPinHandler.Android.cs#L14

        public static void MapLocation(IMapPinHandler handler, IMapPin mapPin)
        {
            if (mapPin.Location != null)
                handler.PlatformView.SetPosition(new LatLng(mapPin.Location.Latitude, mapPin.Location.Longitude));
        }

The handler maintains a MarkerOptions as its PlatformView and loses track of the Marker instance created in the MapHandler. So updating the Pin's Location property updates the MarkerOptions, which has already been used to create the Marker and is therefore no longer useful. The handler should keep track of the created Marker (which is present in a private list on the MapHandler) and update its Position property directly.

jfversluis commented 5 months ago

@mfeingol thanks for the investigation here! Would you also maybe be able to provide a PR? Or is that not something you would want to help out with? 😄