-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
base: main
Are you sure you want to change the base?
Changes from all commits
6d3c1ce
18847ca
f84e804
a98becd
f11b4d1
d53c629
8a04fa5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider using early return pattern consistently. The original code used
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
{ | ||||||
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); | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
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 concreteMapPinHandler
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.