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 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
Text="Add Pin"
Clicked="OnAddPinClicked" />
<Button
Text="Move Pin"
Clicked="OnMovePinClicked"/>
<Button
Text="Remove Pin"
Clicked="OnRemovePinClicked" />
<Button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using Microsoft.Maui.Controls.Maps;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Maps;
using Position = Microsoft.Maui.Devices.Sensors.Location;

namespace Maui.Controls.Sample.Pages.MapsGalleries
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapPinsGallery
{
const double DefaultMapRadiusKm = 5.0;
readonly Random _locationRandomSeed = new();
int _locationIncrement = 0;

Expand Down Expand Up @@ -81,6 +83,11 @@ void OnAddPinClicked(object sender, EventArgs e)
AddPin();
}

void OnMovePinClicked(object sender, EventArgs e)
{
MovePin();
}

void OnRemovePinClicked(object sender, EventArgs e)
{
if (pinsMap.Pins.Count > 0)
Expand All @@ -100,13 +107,34 @@ void OnAdd10PinsClicked(object sender, EventArgs e)

void AddPin()
{
pinsMap.Pins.Add(new Pin()
var randomLocation = GetRandomLocation();
var pin = new Pin
{
Label = $"Location {_locationIncrement++}",
Location = _randomLocations[_locationRandomSeed.Next(0, _randomLocations.Length)],
});
Location = randomLocation,
};
pinsMap.Pins.Add(pin);
MoveMapTo(randomLocation);
}

void MovePin()
{
if (pinsMap.Pins.Count == 0)
{
return;
}

var randomLocation = GetRandomLocation();
pinsMap.Pins[0].Location = randomLocation;
MoveMapTo(randomLocation);
}

Position GetRandomLocation() =>
_randomLocations[_locationRandomSeed.Next(_randomLocations.Length)];

void MoveMapTo(Position location) =>
pinsMap.MoveToRegion(MapSpan.FromCenterAndRadius(location, Distance.FromKilometers(DefaultMapRadiusKm)));

void OnMapClicked(object sender, MapClickedEventArgs e)
{
DisplayAlert("Map", $"Map {e.Location.Latitude}, {e.Location.Longitude} clicked.", "Ok");
Expand Down
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)
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change narrows the type check from IMapPinHandler to the concrete MapPinHandler class. This is potentially a breaking change as it restricts the interface to only work with the specific implementation, breaking the abstraction that the interface provides. Consider maintaining the interface-based approach while still accessing the Marker property.

Copilot uses AI. Check for mistakes.

{
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
47 changes: 44 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,68 @@
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a DisconnectHandler override to properly clean up the weak reference when the handler is disconnected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz , Added DisconnectHandler. Please let me know if you have any concerns.

{
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();

protected override void DisconnectHandler(MarkerOptions platformView)
{
// Clean up the weak reference to avoid potential memory leaks
_markerWeakReference = null;
base.DisconnectHandler(platformView);
}

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)
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using early return pattern consistently. The original code used != null check, but this change uses is null. For consistency within the codebase, maintain the same null-checking pattern unless there's a specific reason to change it.

Suggested change
if (mapPin.Location is null)
if (mapPin.Location == null)

Copilot uses AI. Check for mistakes.

{
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.MapElementHandlerUpdate(Mic
override Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.Equals(object? obj) -> bool
override Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.GetHashCode() -> int
override Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.ToString() -> string!
override Microsoft.Maui.Maps.Handlers.MapPinHandler.DisconnectHandler(Android.Gms.Maps.Model.MarkerOptions! platformView) -> void
static Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.operator !=(Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate? left, Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate? right) -> bool
static Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.operator ==(Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate? left, Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate? right) -> bool
virtual Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.<Clone>$() -> Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate!
virtual Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.Equals(Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate? other) -> bool
virtual Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.EqualityContract.get -> System.Type!
virtual Microsoft.Maui.Maps.Handlers.MapElementHandlerUpdate.PrintMembers(System.Text.StringBuilder! builder) -> bool