diff options
Diffstat (limited to 'Xamarin.Forms.Maps.iOS')
-rw-r--r-- | Xamarin.Forms.Maps.iOS/FormsMaps.cs | 7 | ||||
-rw-r--r-- | Xamarin.Forms.Maps.iOS/GeocoderBackend.cs | 35 | ||||
-rw-r--r-- | Xamarin.Forms.Maps.iOS/MapPool.cs | 4 | ||||
-rw-r--r-- | Xamarin.Forms.Maps.iOS/MapRenderer.cs | 76 |
4 files changed, 102 insertions, 20 deletions
diff --git a/Xamarin.Forms.Maps.iOS/FormsMaps.cs b/Xamarin.Forms.Maps.iOS/FormsMaps.cs index a87cb8dc..0f5df423 100644 --- a/Xamarin.Forms.Maps.iOS/FormsMaps.cs +++ b/Xamarin.Forms.Maps.iOS/FormsMaps.cs @@ -1,11 +1,16 @@ +#if __MOBILE__ using UIKit; using Xamarin.Forms.Maps.iOS; +#else +using Xamarin.Forms.Maps.MacOS; +#endif namespace Xamarin { public static class FormsMaps { static bool s_isInitialized; +#if __MOBILE__ static bool? s_isiOs8OrNewer; static bool? s_isiOs9OrNewer; static bool? s_isiOs10OrNewer; @@ -39,7 +44,7 @@ namespace Xamarin return s_isiOs10OrNewer.Value; } } - +#endif public static void Init() { if (s_isInitialized) diff --git a/Xamarin.Forms.Maps.iOS/GeocoderBackend.cs b/Xamarin.Forms.Maps.iOS/GeocoderBackend.cs index 08ffbbec..e5d1b25c 100644 --- a/Xamarin.Forms.Maps.iOS/GeocoderBackend.cs +++ b/Xamarin.Forms.Maps.iOS/GeocoderBackend.cs @@ -1,10 +1,22 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using AddressBookUI; +using Contacts; using CoreLocation; +#if __MOBILE__ +using AddressBookUI; +using CCLGeocoder = CoreLocation.CLGeocoder; +#else +using Xamarin.Forms.Maps.MacOS.Extra; +using CCLGeocoder = Xamarin.Forms.Maps.MacOS.Extra.CLGeocoder; +#endif + +#if __MOBILE__ namespace Xamarin.Forms.Maps.iOS +#else +namespace Xamarin.Forms.Maps.MacOS +#endif { internal class GeocoderBackend { @@ -17,21 +29,36 @@ namespace Xamarin.Forms.Maps.iOS static Task<IEnumerable<string>> GetAddressesForPositionAsync(Position position) { var location = new CLLocation(position.Latitude, position.Longitude); - var geocoder = new CLGeocoder(); + var geocoder = new CCLGeocoder(); var source = new TaskCompletionSource<IEnumerable<string>>(); geocoder.ReverseGeocodeLocation(location, (placemarks, error) => { if (placemarks == null) placemarks = new CLPlacemark[0]; - IEnumerable<string> addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false)); + List<string> addresses = new List<string>(); +#if __MOBILE__ + addresses = placemarks.Select(p => ABAddressFormatting.ToString(p.AddressDictionary, false)).ToList(); +#else + foreach (var item in placemarks) + { + var address = new CNMutablePostalAddress(); + address.Street = item.AddressDictionary["Street"] == null ? "" : item.AddressDictionary["Street"].ToString(); + address.State = item.AddressDictionary["State"] == null ? "" : item.AddressDictionary["State"].ToString(); + address.City = item.AddressDictionary["City"] == null ? "" : item.AddressDictionary["City"].ToString(); + address.Country = item.AddressDictionary["Country"] == null ? "" : item.AddressDictionary["Country"].ToString(); + address.PostalCode = item.AddressDictionary["ZIP"] == null ? "" : item.AddressDictionary["ZIP"].ToString(); + addresses.Add(CNPostalAddressFormatter.GetStringFrom(address, CNPostalAddressFormatterStyle.MailingAddress)); + } +#endif source.SetResult(addresses); + }); return source.Task; } static Task<IEnumerable<Position>> GetPositionsForAddressAsync(string address) { - var geocoder = new CLGeocoder(); + var geocoder = new CCLGeocoder(); var source = new TaskCompletionSource<IEnumerable<Position>>(); geocoder.GeocodeAddress(address, (placemarks, error) => { diff --git a/Xamarin.Forms.Maps.iOS/MapPool.cs b/Xamarin.Forms.Maps.iOS/MapPool.cs index dc8a9c47..1b3823c7 100644 --- a/Xamarin.Forms.Maps.iOS/MapPool.cs +++ b/Xamarin.Forms.Maps.iOS/MapPool.cs @@ -1,7 +1,11 @@ using System.Collections.Concurrent; using MapKit; +#if __MOBILE__ namespace Xamarin.Forms.Maps.iOS +#else +namespace Xamarin.Forms.Maps.MacOS +#endif { // A static pool of MKMapView instances we can reuse internal class MapPool 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; } |