diff options
Diffstat (limited to 'Xamarin.Forms.Maps.iOS/MapRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Maps.iOS/MapRenderer.cs | 76 |
1 files changed, 61 insertions, 15 deletions
diff --git a/Xamarin.Forms.Maps.iOS/MapRenderer.cs b/Xamarin.Forms.Maps.iOS/MapRenderer.cs index 794a4a04..0616fa9b 100644 --- a/Xamarin.Forms.Maps.iOS/MapRenderer.cs +++ b/Xamarin.Forms.Maps.iOS/MapRenderer.cs @@ -5,21 +5,27 @@ using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; using CoreLocation; -using Foundation; using MapKit; using ObjCRuntime; -using UIKit; -using Xamarin.Forms.Platform.iOS; using RectangleF = CoreGraphics.CGRect; +using Foundation; +#if __MOBILE__ +using UIKit; +using Xamarin.Forms.Platform.iOS; namespace Xamarin.Forms.Maps.iOS +#else +using AppKit; +using Xamarin.Forms.Platform.MacOS; +namespace Xamarin.Forms.Maps.MacOS +#endif { internal class MapDelegate : MKMapViewDelegate { // keep references alive, removing this will cause a segfault readonly List<object> List = new List<object>(); Map _map; - UIView _lastTouchedView; + object _lastTouchedView; bool _disposed; internal MapDelegate(Map map) @@ -49,9 +55,10 @@ namespace Xamarin.Forms.Maps.iOS return mapPin; } - +#if __MOBILE__ void AttachGestureToPin(MKPinAnnotationView mapPin, IMKAnnotation annotation) { + UIGestureRecognizer[] recognizers = mapPin.GestureRecognizers; if (recognizers != null) @@ -71,9 +78,33 @@ namespace Xamarin.Forms.Maps.iOS List.Add(action); List.Add(recognizer); mapPin.AddGestureRecognizer(recognizer); - } + } +#else + void AttachGestureToPin(MKPinAnnotationView mapPin, IMKAnnotation annotation) + { + NSGestureRecognizer[] recognizers = mapPin.GestureRecognizers; + + if (recognizers != null) + { + foreach (NSGestureRecognizer r in recognizers) + { + mapPin.RemoveGestureRecognizer(r); + } + } + + Action<NSClickGestureRecognizer> action = g => OnClick(annotation, g); + var recognizer = new NSClickGestureRecognizer(action); + List.Add(action); + List.Add(recognizer); + mapPin.AddGestureRecognizer(recognizer); + } +#endif +#if __MOBILE__ void OnClick(object annotationObject, UITapGestureRecognizer recognizer) +#else + void OnClick(object annotationObject, NSClickGestureRecognizer recognizer) +#endif { // https://bugzilla.xamarin.com/show_bug.cgi?id=26416 NSObject annotation = Runtime.GetNSObject(((IMKAnnotation)annotationObject).Handle); @@ -126,9 +157,9 @@ namespace Xamarin.Forms.Maps.iOS public class MapRenderer : ViewRenderer { - CLLocationManager _locationManager; + CLLocationManager _locationManager; bool _shouldUpdateRegion; - bool _disposed; + bool _disposed; const string MoveMessageName = "MapMoveToRegion"; @@ -142,8 +173,10 @@ namespace Xamarin.Forms.Maps.iOS // as much as possible to prevent creating new ones and losing more memory // For the time being, we don't want ViewRenderer handling disposal of the MKMapView - // if we're on iOS 9 or 10; during Dispose we'll be putting the MKMapView in a pool instead + // if we're on iOS 10; during Dispose we'll be putting the MKMapView in a pool instead +#if __MOBILE__ protected override bool ManageNativeControlLifetime => !FormsMaps.IsiOs9OrNewer; +#endif protected override void Dispose(bool disposing) { @@ -169,16 +202,15 @@ namespace Xamarin.Forms.Maps.iOS mkMapView.Delegate.Dispose(); mkMapView.Delegate = null; mkMapView.RemoveFromSuperview(); - +#if __MOBILE__ if (FormsMaps.IsiOs9OrNewer) { // This renderer is done with the MKMapView; we can put it in the pool // for other rendererers to use in the future MapPool.Add(mkMapView); } - +#endif // For iOS versions < 9, the MKMapView will be disposed in ViewRenderer's Dispose method - if (_locationManager != null) { _locationManager.Dispose(); @@ -207,13 +239,13 @@ namespace Xamarin.Forms.Maps.iOS if (Control == null) { MKMapView mapView = null; - +#if __MOBILE__ if (FormsMaps.IsiOs9OrNewer) { // See if we've got an MKMapView available in the pool; if so, use it mapView = MapPool.Get(); } - +#endif if (mapView == null) { // If this is iOS 8 or lower, or if there weren't any MKMapViews in the pool, @@ -260,9 +292,22 @@ namespace Xamarin.Forms.Maps.iOS _shouldUpdateRegion = true; } +#if __MOBILE__ public override void LayoutSubviews() { base.LayoutSubviews(); + UpdateRegion(); + } +#else + public override void Layout() + { + base.Layout(); + UpdateRegion(); + } +#endif + + void UpdateRegion() + { if (_shouldUpdateRegion) { MoveToRegion(((Map)Element).LastMoveToRegion, false); @@ -343,12 +388,13 @@ namespace Xamarin.Forms.Maps.iOS void UpdateIsShowingUser() { +#if __MOBILE__ if (FormsMaps.IsiOs8OrNewer && ((Map)Element).IsShowingUser) { _locationManager = new CLLocationManager(); _locationManager.RequestWhenInUseAuthorization(); } - +#endif ((MKMapView)Control).ShowsUserLocation = ((Map)Element).IsShowingUser; } |