Ejemplo n.º 1
0
        protected override void OnElementChanged(ElementChangedEventArgs <View> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                var mapModel = (Map)e.OldElement;
                MessagingCenter.Unsubscribe <Map, MapSpan>(this, MoveMessageName);
                ((ObservableCollection <Pin>)mapModel.Pins).CollectionChanged -= OnCollectionChanged;

                foreach (Pin pin in mapModel.Pins)
                {
                    pin.PropertyChanged -= PinOnPropertyChanged;
                }
            }

            if (e.NewElement != null)
            {
                var mapModel = (Map)e.NewElement;

                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,
                        // create a new one
                        mapView = new MKMapView(RectangleF.Empty);
                    }

                    SetNativeControl(mapView);

                    mapView.GetViewForAnnotation = GetViewForAnnotation;
                    mapView.RegionChanged       += MkMapViewOnRegionChanged;
                }

                MessagingCenter.Subscribe <Map, MapSpan>(this, MoveMessageName, (s, a) => MoveToRegion(a), mapModel);
                if (mapModel.LastMoveToRegion != null)
                {
                    MoveToRegion(mapModel.LastMoveToRegion, false);
                }

                UpdateMapType();
                UpdateIsShowingUser();
                UpdateHasScrollEnabled();
                UpdateHasZoomEnabled();

                ((ObservableCollection <Pin>)mapModel.Pins).CollectionChanged += OnCollectionChanged;

                OnCollectionChanged(((Map)Element).Pins, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }
Ejemplo n.º 2
0
        protected override void OnElementChanged(ElementChangedEventArgs <View> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                var mapModel = (Map)e.OldElement;

                MessagingCenter.Unsubscribe <Map, MapSpan>(this, MoveMessageName);

                ((ObservableCollection <Pin>)mapModel.Pins).CollectionChanged -= OnPinCollectionChanged;
                foreach (Pin pin in mapModel.Pins)
                {
                    pin.PropertyChanged -= PinOnPropertyChanged;
                }

                ((ObservableCollection <MapElement>)mapModel.MapElements).CollectionChanged -= OnMapElementCollectionChanged;
                foreach (MapElement mapElement in mapModel.MapElements)
                {
                    mapElement.PropertyChanged -= MapElementPropertyChanged;
                }
            }

            if (e.NewElement != null)
            {
                var mapModel = (Map)e.NewElement;

                if (Control == null)
                {
                    MKMapView mapView = null;
#if __MOBILE__
                    // 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,
                        // create a new one
                        mapView = new MKMapView(RectangleF.Empty);
                    }

                    SetNativeControl(mapView);

                    mapView.GetViewForAnnotation     = GetViewForAnnotation;
                    mapView.OverlayRenderer          = GetViewForOverlay;
                    mapView.DidSelectAnnotationView += MkMapViewOnAnnotationViewSelected;
                    mapView.RegionChanged           += MkMapViewOnRegionChanged;
#if __MOBILE__
                    mapView.AddGestureRecognizer(_mapClickedGestureRecognizer = new UITapGestureRecognizer(OnMapClicked));
#endif
                }

                MessagingCenter.Subscribe <Map, MapSpan>(this, MoveMessageName, (s, a) => MoveToRegion(a), mapModel);
                if (mapModel.LastMoveToRegion != null)
                {
                    MoveToRegion(mapModel.LastMoveToRegion, false);
                }

                UpdateTrafficEnabled();
                UpdateMapType();
                UpdateIsShowingUser();
                UpdateHasScrollEnabled();
                UpdateHasZoomEnabled();

                ((ObservableCollection <Pin>)mapModel.Pins).CollectionChanged += OnPinCollectionChanged;
                OnPinCollectionChanged(mapModel.Pins, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

                ((ObservableCollection <MapElement>)mapModel.MapElements).CollectionChanged += OnMapElementCollectionChanged;
                OnMapElementCollectionChanged(mapModel.MapElements, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }