Пример #1
0
        protected override void OnElementChanged(ElementChangedEventArgs <View> e)
        {
            base.OnElementChanged(e);

            // For XAML Previewer or FormsGoogleMaps.Init not called.
            if (!FormsGoogleMaps.IsInitialized)
            {
                var label = new UILabel()
                {
                    Text            = "Xamarin.Forms.GoogleMaps",
                    BackgroundColor = Color.Teal.ToUIColor(),
                    TextColor       = Color.Black.ToUIColor(),
                    TextAlignment   = UITextAlignment.Center
                };
                SetNativeControl(label);
                return;
            }

            var oldMapView = (MapView)Control;

            if (e.OldElement != null)
            {
                _cameraLogic.Unregister();
            }

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

                if (Control == null)
                {
                    SetNativeControl(new MapView(RectangleF.Empty));
                    var mkMapView = (MapView)Control;
                    mkMapView.CameraPositionChanged += CameraPositionChanged;
                    mkMapView.CoordinateTapped      += CoordinateTapped;
                    mkMapView.CoordinateLongPressed += CoordinateLongPressed;
                    mkMapView.DidTapMyLocationButton = DidTapMyLocation;
                }

                _cameraLogic.Register(Map, NativeMap);

                if (mapModel.LastMoveToRegion != null)
                {
                    _cameraLogic.MoveToRegion(mapModel.LastMoveToRegion, false);
                }

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

                foreach (var logic in _logics)
                {
                    logic.Register(oldMapView, (Map)e.OldElement, NativeMap, Map);
                    logic.RestoreItems();
                    logic.OnMapPropertyChanged(new PropertyChangedEventArgs(Map.SelectedPinProperty.PropertyName));
                }
            }
        }
        async Task UpdateVisibleRegion()
        {
            if (Control == null || Element == null)
            {
                return;
            }

            if (!_firstZoomLevelChangeFired)
            {
                await _cameraLogic.MoveToRegion(Element.LastMoveToRegion, MapAnimationKind.None);

                _firstZoomLevelChangeFired = true;
                return;
            }
            Geopoint nw, se = null;

            try
            {
                Control.GetLocationFromOffset(new Windows.Foundation.Point(0, 0), out nw);
                Control.GetLocationFromOffset(new Windows.Foundation.Point(Control.ActualWidth, Control.ActualHeight), out se);
            }
            catch (Exception)
            {
                return;
            }

            if (nw != null && se != null)
            {
                var boundingBox    = new GeoboundingBox(nw.Position, se.Position);
                var center         = new Position(boundingBox.Center.Latitude, boundingBox.Center.Longitude);
                var latitudeDelta  = Math.Abs(center.Latitude - boundingBox.NorthwestCorner.Latitude);
                var longitudeDelta = Math.Abs(center.Longitude - boundingBox.NorthwestCorner.Longitude);
                Element.VisibleRegion = new MapSpan(center, latitudeDelta, longitudeDelta);
            }
        }
        void InitializeLogic()
        {
            _cameraLogic.MoveToRegion(((Map)Element).LastMoveToRegion, false);

            foreach (var logic in _logics)
            {
                if (logic.Map != null)
                {
                    logic.RestoreItems();
                    logic.OnMapPropertyChanged(new PropertyChangedEventArgs(Map.SelectedPinProperty.PropertyName));
                }
            }

            _ready    = false;
            _onLayout = false;
        }