Exemplo n.º 1
0
        public static MGLMapCamera ToNative(this CameraPosition cameraPosition, CGSize size)
        {
            var heading          = cameraPosition.Bearing ?? 0;
            var centerCoordinate = cameraPosition.Target ?? LatLng.Zero;
            var pitch            = (nfloat)(cameraPosition.Tilt ?? 0);
            var altitude         = 0.0;

            // TODO iOS Convert Zoom to Altitude
            if (cameraPosition.Zoom.HasValue)
            {
                var result = MapboxIndependentFunction.MGLAltitudeForZoomLevel(
                    cameraPosition.Zoom.Value,
                    (nfloat)(cameraPosition.Tilt ?? 0),
                    cameraPosition.Target?.Lat ?? 0,
                    size);
                altitude = result;
            }

            var camera = MGLMapCamera.CameraLookingAtCenterCoordinateAndAltitude(
                centerCoordinate.ToCLCoordinate(),
                altitude,
                pitch,
                heading
                );

            return(camera);
        }
Exemplo n.º 2
0
        public static MGLMapCamera ToNative(this CameraBounds cameraBounds, MGLMapView map)
        {
            var camera = map.CameraThatFitsCoordinateBounds(
                cameraBounds.Bounds.ToNative(),
                cameraBounds.Padding.ToEdgeInsets()
                );

            var heading = cameraBounds.Bearing ?? camera.Heading;
            var pitch   = (nfloat)(cameraBounds.Tilt ?? camera.Pitch);

            var result = MGLMapCamera.CameraLookingAtCenterCoordinateAndAltitude(
                camera.CenterCoordinate,
                camera.Altitude,
                pitch,
                heading
                );

            return(result);
        }
Exemplo n.º 3
0
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (MapView == null || Element == null)
            {
                return;
            }

            if (e.PropertyName == FormsMap.CenterProperty.PropertyName)
            {
                UpdateCenter();
            }
            else if (e.PropertyName == FormsMap.ZoomLevelProperty.PropertyName &&
                     !Math.Round(Element.ZoomLevel * 100).Equals(Math.Round(MapView.ZoomLevel * 100)))
            {
                //MapView.SetZoomLevel(Element.ZoomLevel, true);
                MapView.ZoomLevel = Element.ZoomLevel;
            }
            else if (e.PropertyName == FormsMap.PitchEnabledProperty.PropertyName && MapView.PitchEnabled != Element.PitchEnabled)
            {
                MapView.PitchEnabled = Element.PitchEnabled;
            }
            else if (e.PropertyName == FormsMap.RotateEnabledProperty.PropertyName && MapView.RotateEnabled != Element.RotateEnabled)
            {
                MapView.RotateEnabled = Element.RotateEnabled;
            }
            else if (e.PropertyName == FormsMap.AnnotationsProperty.PropertyName)
            {
                if (Element.Annotations != null)
                {
                    AddAnnotations(Element.Annotations.ToArray());
                    var notifyCollection = Element.Annotations as INotifyCollectionChanged;
                    if (notifyCollection != null)
                    {
                        notifyCollection.CollectionChanged += OnAnnotationsCollectionChanged;
                    }
                }
            }
            //else if (e.PropertyName == FormsMap.StyleUrlProperty.PropertyName
            //		 && !string.IsNullOrEmpty(Element.StyleUrl)
            //		   && (MapView.StyleURL == null
            //			   || MapView.StyleURL.AbsoluteString != Element.StyleUrl))
            //{
            //	MapView.StyleURL = new NSUrl(Element.StyleUrl);
            //}
            else if (e.PropertyName == FormsMap.MapStyleProperty.PropertyName &&
                     Element.MapStyle != null &&
                     !string.IsNullOrEmpty(Element.MapStyle.UrlString) &&
                     (MapView.StyleURL == null ||
                      MapView.StyleURL.AbsoluteString != Element.MapStyle.UrlString))
            {
                UpdateMapStyle();
            }
            else if (e.PropertyName == FormsMap.PitchProperty.PropertyName &&
                     !Element.Pitch.Equals(MapView.Camera.Pitch))
            {
                var currentCamera = MapView.Camera;
                var newCamera     = MGLMapCamera.CameraLookingAtCenterCoordinate(currentCamera.CenterCoordinate,
                                                                                 currentCamera.Altitude,
                                                                                 (nfloat)Element.Pitch,
                                                                                 currentCamera.Heading);
                MapView.SetCamera(newCamera, true);
            }
            else if (e.PropertyName == FormsMap.RotatedDegreeProperty.PropertyName &&
                     !Element.RotatedDegree.Equals(MapView.Camera.Heading))
            {
                var currentCamera = MapView.Camera;
                var newCamera     = MGLMapCamera.CameraLookingAtCenterCoordinate(currentCamera.CenterCoordinate,
                                                                                 currentCamera.Altitude,
                                                                                 currentCamera.Pitch,
                                                                                 (nfloat)Element.RotatedDegree);
                MapView.SetCamera(newCamera, true);
            }
        }
Exemplo n.º 4
0
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (map == null || Element == null)
            {
                return;
            }

            if (e.PropertyName == MapView.VisibleBoundsProperty.PropertyName)
            {
                UpdateRegion();
            }
            else if (e.PropertyName == MapView.CenterProperty.PropertyName)
            {
                UpdateCenter();
            }
            else if (e.PropertyName == MapView.ZoomLevelProperty.PropertyName)
            {
                if (Element.ZoomLevel.HasValue == false)
                {
                    return;                                      // TODO Set to default value
                }
                var isSameLevel = Math.Round(Element.ZoomLevel.Value * 100).Equals(Math.Round(map.ZoomLevel * 100));

                if (isSameLevel)
                {
                    return;
                }

                map.ZoomLevel = Element.ZoomLevel.Value;
            }
            else if (e.PropertyName == MapView.PitchEnabledProperty.PropertyName)
            {
                if (map.PitchEnabled == Element.PitchEnabled)
                {
                    return;
                }

                map.PitchEnabled = Element.PitchEnabled;
            }
            else if (e.PropertyName == MapView.ScrollEnabledProperty.PropertyName)
            {
                if (map.ScrollEnabled == Element.ScrollEnabled)
                {
                    return;
                }

                map.ScrollEnabled = Element.ScrollEnabled;
            }
            else if (e.PropertyName == MapView.RotateEnabledProperty.PropertyName)
            {
                if (map.RotateEnabled == Element.RotateEnabled)
                {
                    return;
                }

                map.RotateEnabled = Element.RotateEnabled;
            }
            else if (e.PropertyName == MapView.AnnotationsProperty.PropertyName)
            {
                if (Element.Annotations == null || Element.Annotations.Count() == 0)
                {
                    return;
                }

                AddAnnotations(Element.Annotations.ToArray());

                if (Element.Annotations is INotifyCollectionChanged observable)
                {
                    observable.CollectionChanged -= OnAnnotationsCollectionChanged;
                    observable.CollectionChanged += OnAnnotationsCollectionChanged;
                }
            }
            else if (e.PropertyName == MapView.MapStyleProperty.PropertyName)
            {
                var shouldNotUpdate = string.IsNullOrWhiteSpace(Element.MapStyle?.UrlString) ||
                                      string.Equals(Element.MapStyle.UrlString, map.StyleURL?.AbsoluteString);

                if (shouldNotUpdate)
                {
                    return;
                }

                UpdateMapStyle();
            }
            else if (e.PropertyName == MapView.PitchProperty.PropertyName)
            {
                var shouldNotUpdate = Math.Abs(Element.Pitch - map.Camera.Pitch) < 0.05;

                if (shouldNotUpdate)
                {
                    return;
                }

                var currentCamera = map.Camera;
                var newCamera     = MGLMapCamera.CameraLookingAtCenterCoordinateAndAltitude(
                    currentCamera.CenterCoordinate,
                    currentCamera.Altitude,
                    (nfloat)Element.Pitch,
                    currentCamera.Heading);
                map.SetCamera(newCamera, true);
            }
            else if (e.PropertyName == MapView.RotatedDegreeProperty.PropertyName)
            {
                var shouldNotUpdate = Math.Abs(Element.RotatedDegree - map.Camera.Heading) < 0.05;

                if (shouldNotUpdate)
                {
                    return;
                }

                var currentCamera = map.Camera;
                var newCamera     = MGLMapCamera.CameraLookingAtCenterCoordinateAndAltitude(
                    currentCamera.CenterCoordinate,
                    currentCamera.Altitude,
                    currentCamera.Pitch,
                    (nfloat)Element.RotatedDegree);
                map.SetCamera(newCamera, true);
            }
            else if (e.PropertyName == MapView.ShowUserLocationProperty.PropertyName)
            {
                map.ShowsUserLocation = Element.ShowUserLocation;
            }
        }