Skip to content

Fix Changing Location on a Pin does nothing #30201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Core/maps/src/Handlers/Map/MapHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,20 @@ void AddPins(IList pins)
Marker? marker;

var pinHandler = pin.ToHandler(MauiContext);
if (pinHandler is IMapPinHandler iMapPinHandler)
if (pinHandler is MapPinHandler mapPinHandler)
{
marker = Map.AddMarker(iMapPinHandler.PlatformView);
marker = Map.AddMarker(mapPinHandler.PlatformView);
if (marker == null)
{
throw new System.Exception("Map.AddMarker returned null");
}

// Store the marker reference in the MapPinHandler for future property updates
mapPinHandler.Marker = marker;

// associate pin with marker for later lookup in event handlers
pin.MarkerId = marker.Id;
_markers.Add(marker!);
_markers.Add(marker);
}

}
Expand Down
40 changes: 37 additions & 3 deletions src/Core/maps/src/Handlers/MapPin/MapPinHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
using Android.Gms.Maps;
using System;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Microsoft.Maui.Handlers;

namespace Microsoft.Maui.Maps.Handlers
{
public partial class MapPinHandler : ElementHandler<IMapPin, MarkerOptions>
{
// Keep track of the actual marker associated with this handler using a weak reference
// to avoid potential memory leaks (the Marker is owned by the Google Maps view)
WeakReference<Marker>? _markerWeakReference;

internal Marker? Marker
{
get => _markerWeakReference?.TryGetTarget(out var marker) == true ? marker : null;
set => _markerWeakReference = value is not null ? new WeakReference<Marker>(value) : null;
}

protected override MarkerOptions CreatePlatformElement() => new MarkerOptions();

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

// Always update the MarkerOptions
var position = new LatLng(mapPin.Location.Latitude, mapPin.Location.Longitude);
handler.PlatformView.SetPosition(position);

// Update the actual marker if available
UpdateMarker(handler, marker => marker.Position = position);
}

public static void MapLabel(IMapPinHandler handler, IMapPin mapPin)
{
handler.PlatformView.SetTitle(mapPin.Label);

// Update the actual marker if available
UpdateMarker(handler, marker => marker.Title = mapPin.Label);
}

public static void MapAddress(IMapPinHandler handler, IMapPin mapPin)
{
handler.PlatformView.SetSnippet(mapPin.Address);

// Update the actual marker if available
UpdateMarker(handler, marker => marker.Snippet = mapPin.Address);
}

static void UpdateMarker(IMapPinHandler handler, Action<Marker> updateAction)
{
if (handler is MapPinHandler mapPinHandler && mapPinHandler.Marker is Marker marker)
{
updateAction(marker);
}
}
}
}