-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5246106
commit 429211c
Showing
8 changed files
with
217 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
using Microsoft.Maui.Controls.Maps; | ||
using Microsoft.Maui.Maps; | ||
using Microsoft.Maui.Maps.Handlers; | ||
using Microsoft.Maui.Maps.Platform; | ||
using Microsoft.Maui.Platform; | ||
using IMap = Microsoft.Maui.Maps.IMap; | ||
#if IOS || MACCATALYST | ||
using UIKit; | ||
using MapKit; | ||
using CoreLocation; | ||
#endif | ||
|
||
namespace AnchorAlertApp; | ||
// https://github.com/VladislavAntonyuk/MauiSamples/tree/main/MauiMaps | ||
public class AnchorPin : Pin | ||
{ | ||
public static readonly BindableProperty ImageSourceProperty = BindableProperty.Create( | ||
nameof(ImageSource), | ||
typeof(ImageSource), | ||
typeof(AnchorPin)); | ||
|
||
public ImageSource? ImageSource | ||
{ | ||
get => (ImageSource?)GetValue(ImageSourceProperty); | ||
set => SetValue(ImageSourceProperty, value); | ||
} | ||
} | ||
|
||
#if IOS || MACCATALYST | ||
public class CustomAnnotation : MKPointAnnotation | ||
{ | ||
public Guid Identifier { get; init; } | ||
public UIImage? Image { get; init; } | ||
public required IMapPin Pin { get; init; } | ||
} | ||
public class AnchorMapHandler : MapHandler | ||
{ | ||
private static UIView? lastTouchedView; | ||
public static readonly IPropertyMapper<IMap, IMapHandler> CustomMapper = | ||
new PropertyMapper<IMap, IMapHandler>(Mapper) | ||
{ | ||
[nameof(IMap.Pins)] = MapPins, | ||
}; | ||
|
||
public AnchorMapHandler() : base(CustomMapper, CommandMapper) | ||
{ | ||
} | ||
|
||
public AnchorMapHandler(IPropertyMapper? mapper = null, CommandMapper? commandMapper = null) : base( | ||
mapper ?? CustomMapper, commandMapper ?? CommandMapper) | ||
{ | ||
} | ||
|
||
public List<IMKAnnotation> Markers { get; } = new(); | ||
|
||
protected override void ConnectHandler(MauiMKMapView platformView) | ||
{ | ||
base.ConnectHandler(platformView); | ||
platformView.GetViewForAnnotation += GetViewForAnnotations; | ||
} | ||
|
||
private static void OnCalloutClicked(IMKAnnotation annotation) | ||
{ | ||
var pin = GetPinForAnnotation(annotation); | ||
if (lastTouchedView is MKAnnotationView) | ||
return; | ||
pin?.SendInfoWindowClick(); | ||
} | ||
|
||
private static MKAnnotationView GetViewForAnnotations(MKMapView mapView, IMKAnnotation annotation) | ||
{ | ||
MKAnnotationView annotationView; | ||
if (annotation is CustomAnnotation customAnnotation) | ||
{ | ||
annotationView = mapView.DequeueReusableAnnotation(customAnnotation.Identifier.ToString()) ?? | ||
new MKAnnotationView(annotation, customAnnotation.Identifier.ToString()); | ||
annotationView.Image = customAnnotation.Image; | ||
annotationView.CanShowCallout = true; | ||
} | ||
else if (annotation is MKPointAnnotation) | ||
{ | ||
annotationView = mapView.DequeueReusableAnnotation("defaultPin") ?? | ||
new MKMarkerAnnotationView(annotation, "defaultPin"); | ||
annotationView.CanShowCallout = true; | ||
} | ||
else | ||
{ | ||
annotationView = new MKUserLocationView(annotation, null); | ||
} | ||
|
||
var result = annotationView ?? new MKAnnotationView(annotation, null); | ||
AttachGestureToPin(result, annotation); | ||
return result; | ||
} | ||
|
||
static void AttachGestureToPin(MKAnnotationView mapPin, IMKAnnotation annotation) | ||
{ | ||
var recognizers = mapPin.GestureRecognizers; | ||
|
||
if (recognizers != null) | ||
{ | ||
foreach (var r in recognizers) | ||
{ | ||
mapPin.RemoveGestureRecognizer(r); | ||
} | ||
} | ||
|
||
var recognizer = new UITapGestureRecognizer(g => OnCalloutClicked(annotation)) | ||
{ | ||
ShouldReceiveTouch = (gestureRecognizer, touch) => | ||
{ | ||
lastTouchedView = touch.View; | ||
return true; | ||
} | ||
}; | ||
|
||
mapPin.AddGestureRecognizer(recognizer); | ||
} | ||
|
||
static IMapPin? GetPinForAnnotation(IMKAnnotation? annotation) | ||
{ | ||
if (annotation is CustomAnnotation customAnnotation) | ||
{ | ||
return customAnnotation.Pin; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static new void MapPins(IMapHandler handler, IMap map) | ||
{ | ||
if (handler is AnchorMapHandler mapHandler) | ||
{ | ||
foreach (var marker in mapHandler.Markers) | ||
{ | ||
mapHandler.PlatformView.RemoveAnnotation(marker); | ||
} | ||
|
||
mapHandler.Markers.Clear(); | ||
mapHandler.AddPins(map.Pins); | ||
} | ||
} | ||
|
||
private void AddPins(IEnumerable<IMapPin> mapPins) | ||
{ | ||
if (MauiContext is null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var pin in mapPins) | ||
{ | ||
var pinHandler = pin.ToHandler(MauiContext); | ||
if (pinHandler is IMapPinHandler mapPinHandler) | ||
{ | ||
var markerOption = mapPinHandler.PlatformView; | ||
if (pin is AnchorPin cp) | ||
{ | ||
cp.ImageSource.LoadImage(MauiContext, result => | ||
{ | ||
markerOption = new CustomAnnotation() | ||
{ | ||
Identifier = cp.Id, | ||
Image = result?.Value, | ||
Title = pin.Label, | ||
Subtitle = pin.Address, | ||
Coordinate = new CLLocationCoordinate2D(pin.Location.Latitude, pin.Location.Longitude), | ||
Pin = cp | ||
}; | ||
|
||
AddMarker(PlatformView, pin, Markers, markerOption); | ||
}); | ||
} | ||
else | ||
{ | ||
AddMarker(PlatformView, pin, Markers, markerOption); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void AddMarker(MauiMKMapView map, IMapPin pin, List<IMKAnnotation> markers, IMKAnnotation annotation) | ||
{ | ||
map.AddAnnotation(annotation); | ||
pin.MarkerId = annotation; | ||
markers.Add(annotation); | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.