コード例 #1
0
ファイル: MapView.cs プロジェクト: wjax/Mapsui
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Mapsui.UI.Forms.MapView"/> class.
        /// </summary>
        public MapView()
        {
            MyLocationEnabled = false;
            MyLocationFollow  = false;

            IsClippedToBounds = true;

            _mapControl = new MapControl {
                UseDoubleTap = false
            };
            _mapMyLocationLayer = new MyLocationLayer(this)
            {
                Enabled = true
            };
            _mapPinLayer = new MemoryLayer()
            {
                Name = PinLayerName, IsMapInfoLayer = true
            };
            _mapDrawableLayer = new MemoryLayer()
            {
                Name = DrawableLayerName, IsMapInfoLayer = true
            };

            // Get defaults from MapControl
            RotationLock = Map.RotationLock;
            ZoomLock     = Map.ZoomLock;
            PanLock      = Map.PanLock;

            // Add some events to _mapControl
            _mapControl.Viewport.ViewportChanged += HandlerViewportChanged;
            _mapControl.ViewportInitialized      += HandlerViewportInitialized;
            _mapControl.Info            += HandlerInfo;
            _mapControl.PropertyChanged += HandlerMapControlPropertyChanged;
            _mapControl.SingleTap       += HandlerTap;
            _mapControl.DoubleTap       += HandlerTap;
            _mapControl.LongTap         += HandlerLongTap;
            _mapControl.Hovered         += HandlerHover;
            _mapControl.TouchMove       += (s, e) =>
            {
                Device.BeginInvokeOnMainThread(() => MyLocationFollow = false);
            };

            AbsoluteLayout.SetLayoutBounds(_mapControl, new Rectangle(0, 0, 1, 1));
            AbsoluteLayout.SetLayoutFlags(_mapControl, AbsoluteLayoutFlags.All);

            _pictMyLocationNoCenter = new SkiaSharp.Extended.Svg.SKSvg().Load(Utilities.EmbeddedResourceLoader.Load("Images.LocationNoCenter.svg", typeof(MapView)));
            _pictMyLocationCenter   = new SkiaSharp.Extended.Svg.SKSvg().Load(Utilities.EmbeddedResourceLoader.Load("Images.LocationCenter.svg", typeof(MapView)));

            _mapZoomInButton = new SvgButton(Utilities.EmbeddedResourceLoader.Load("Images.ZoomIn.svg", typeof(MapView)))
            {
                BackgroundColor = Color.White,
                WidthRequest    = 40,
                HeightRequest   = 40,
                Command         = new Command(obj => { _mapControl.Navigator.ZoomIn(); Refresh(); })
            };

            _mapZoomOutButton = new SvgButton(Utilities.EmbeddedResourceLoader.Load("Images.ZoomOut.svg", typeof(MapView)))
            {
                BackgroundColor = Color.White,
                WidthRequest    = 40,
                HeightRequest   = 40,
                Command         = new Command(obj => { _mapControl.Navigator.ZoomOut(); Refresh(); }),
            };

            _mapSpacingButton1 = new Image {
                BackgroundColor = Color.Transparent, WidthRequest = 40, HeightRequest = 8
            };

            _mapMyLocationButton = new SvgButton(_pictMyLocationNoCenter)
            {
                BackgroundColor = Color.White,
                WidthRequest    = 40,
                HeightRequest   = 40,
                Command         = new Command(obj => MyLocationFollow = !MyLocationFollow),
            };

            _mapSpacingButton2 = new Image {
                BackgroundColor = Color.Transparent, WidthRequest = 40, HeightRequest = 8
            };

            _mapNorthingButton = new SvgButton(Utilities.EmbeddedResourceLoader.Load("Images.RotationZero.svg", typeof(MapView)))
            {
                BackgroundColor = Color.White,
                WidthRequest    = 40,
                HeightRequest   = 40,
                Command         = new Command(obj => Device.BeginInvokeOnMainThread(() => _mapControl.Navigator.RotateTo(0))),
            };

            _mapButtons = new StackLayout {
                BackgroundColor = Color.Transparent, Opacity = 0.8, Spacing = 0, IsVisible = true
            };

            _mapButtons.Children.Add(_mapZoomInButton);
            _mapButtons.Children.Add(_mapZoomOutButton);
            _mapButtons.Children.Add(_mapSpacingButton1);
            _mapButtons.Children.Add(_mapMyLocationButton);
            _mapButtons.Children.Add(_mapSpacingButton2);
            _mapButtons.Children.Add(_mapNorthingButton);

            AbsoluteLayout.SetLayoutBounds(_mapButtons, new Rectangle(0.95, 0.03, 40, 176));
            AbsoluteLayout.SetLayoutFlags(_mapButtons, AbsoluteLayoutFlags.PositionProportional);

            Content = new AbsoluteLayout
            {
                Children =
                {
                    _mapControl,
                    _mapButtons,
                }
            };

            _pins.CollectionChanged     += HandlerPinsOnCollectionChanged;
            _drawable.CollectionChanged += HandlerDrawablesOnCollectionChanged;

            _mapPinLayer.DataSource = new ObservableCollectionProvider <Pin>(_pins);
            _mapPinLayer.Style      = null; // We don't want a global style for this layer

            _mapDrawableLayer.DataSource = new ObservableCollectionProvider <Drawable>(_drawable);
            _mapDrawableLayer.Style      = null; // We don't want a global style for this layer
        }
コード例 #2
0
ファイル: MapView.cs プロジェクト: janusw/Mapsui
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Mapsui.UI.Forms.MapView"/> class.
        /// </summary>
        public MapView()
        {
            MyLocationEnabled = false;
            MyLocationFollow  = false;

            IsClippedToBounds = true;

            _mapControl = new MapControl {
                UseDoubleTap = false
            };
            MyLocationLayer = new MyLocationLayer(this)
            {
                Enabled = true
            };
            _mapCalloutLayer = new MemoryLayer()
            {
                Name = CalloutLayerName, IsMapInfoLayer = true
            };
            _mapPinLayer = new MemoryLayer()
            {
                Name = PinLayerName, IsMapInfoLayer = true
            };
            _mapDrawableLayer = new MemoryLayer()
            {
                Name = DrawableLayerName, IsMapInfoLayer = true
            };

            // Get defaults from MapControl
            RotationLock = Map.RotationLock;
            ZoomLock     = Map.ZoomLock;
            PanLock      = Map.PanLock;

            // Add some events to _mapControl
            _mapControl.Viewport.ViewportChanged += HandlerViewportChanged;
            _mapControl.ViewportInitialized      += HandlerViewportInitialized;
            _mapControl.Info            += HandlerInfo;
            _mapControl.PropertyChanged += HandlerMapControlPropertyChanged;
            _mapControl.SingleTap       += HandlerTap;
            _mapControl.DoubleTap       += HandlerTap;
            _mapControl.LongTap         += HandlerLongTap;
            _mapControl.Hovered         += HandlerHovered;
            _mapControl.TouchStarted    += HandlerTouchStarted;
            _mapControl.TouchEnded      += HandlerTouchEnded;
            _mapControl.TouchEntered    += HandlerTouchEntered;
            _mapControl.TouchExited     += HandlerTouchExited;
            _mapControl.TouchMove       += HandlerTouchMove;
            _mapControl.TouchAction     += HandlerTouchAction;
            _mapControl.Swipe           += HandlerSwipe;
            _mapControl.Fling           += HandlerFling;
            _mapControl.Zoomed          += HandlerZoomed;

            _mapControl.TouchMove += (s, e) =>
            {
                Device.BeginInvokeOnMainThread(() => MyLocationFollow = false);
            };

            // Add MapView layers to Map
            AddLayers();

            // Add some events to _mapControl.Map.Layers
            _mapControl.Map.Layers.LayerAdded   += HandlerLayerChanged;
            _mapControl.Map.Layers.LayerMoved   += HandlerLayerChanged;
            _mapControl.Map.Layers.LayerRemoved += HandlerLayerChanged;

            AbsoluteLayout.SetLayoutBounds(_mapControl, new Rectangle(0, 0, 1, 1));
            AbsoluteLayout.SetLayoutFlags(_mapControl, AbsoluteLayoutFlags.All);

            _pictMyLocationNoCenter = new SKSvg().Load(Utilities.EmbeddedResourceLoader.Load("Images.LocationNoCenter.svg", typeof(MapView)));
            _pictMyLocationCenter   = new SKSvg().Load(Utilities.EmbeddedResourceLoader.Load("Images.LocationCenter.svg", typeof(MapView)));

            _mapZoomInButton = new SvgButton(Utilities.EmbeddedResourceLoader.Load("Images.ZoomIn.svg", typeof(MapView)))
            {
                BackgroundColor = Color.Transparent,
                WidthRequest    = 40,
                HeightRequest   = 40,
                Command         = new Command(obj => { _mapControl.Navigator.ZoomIn(); })
            };

            _mapZoomOutButton = new SvgButton(Utilities.EmbeddedResourceLoader.Load("Images.ZoomOut.svg", typeof(MapView)))
            {
                BackgroundColor = Color.Transparent,
                WidthRequest    = 40,
                HeightRequest   = 40,
                Command         = new Command(obj => { _mapControl.Navigator.ZoomOut(); }),
            };

            _mapSpacingButton1 = new Image {
                BackgroundColor = Color.Transparent, WidthRequest = 40, HeightRequest = 8, InputTransparent = true
            };

            _mapMyLocationButton = new SvgButton(_pictMyLocationNoCenter)
            {
                BackgroundColor = Color.Transparent,
                WidthRequest    = 40,
                HeightRequest   = 40,
                Command         = new Command(obj => MyLocationFollow = true),
            };

            _mapSpacingButton2 = new Image {
                BackgroundColor = Color.Transparent, WidthRequest = 40, HeightRequest = 8, InputTransparent = true
            };

            _mapNorthingButton = new SvgButton(Utilities.EmbeddedResourceLoader.Load("Images.RotationZero.svg", typeof(MapView)))
            {
                BackgroundColor = Color.Transparent,
                WidthRequest    = 40,
                HeightRequest   = 40,
                Command         = new Command(obj => Device.BeginInvokeOnMainThread(() => _mapControl.Navigator.RotateTo(0))),
            };

            _mapButtons = new StackLayout {
                BackgroundColor = Color.Transparent, Spacing = 0, IsVisible = true, InputTransparent = true, CascadeInputTransparent = false
            };

            _mapButtons.Children.Add(_mapZoomInButton);
            _mapButtons.Children.Add(_mapZoomOutButton);
            _mapButtons.Children.Add(_mapSpacingButton1);
            _mapButtons.Children.Add(_mapMyLocationButton);
            _mapButtons.Children.Add(_mapSpacingButton2);
            _mapButtons.Children.Add(_mapNorthingButton);

            if (Device.RuntimePlatform == Device.iOS)
            {
                // the buttons are placed a bit lower on iOS to avoid overlap with the transparent status bar
                AbsoluteLayout.SetLayoutBounds(_mapButtons, new Rectangle(0.95, 0.07, 40, 176));
            }
            else
            {
                AbsoluteLayout.SetLayoutBounds(_mapButtons, new Rectangle(0.95, 0.03, 40, 176));
            }

            AbsoluteLayout.SetLayoutFlags(_mapButtons, AbsoluteLayoutFlags.PositionProportional);

            Content = new AbsoluteLayout
            {
                Children =
                {
                    _mapControl,
                    _mapButtons,
                }
            };

            _pins.CollectionChanged     += HandlerPinsOnCollectionChanged;
            _drawable.CollectionChanged += HandlerDrawablesOnCollectionChanged;

            _mapCalloutLayer.DataSource = new ObservableCollectionProvider <Callout>(_callouts);
            _mapCalloutLayer.Style      = null; // We don't want a global style for this layer

            _mapPinLayer.DataSource = new ObservableCollectionProvider <Pin>(_pins);
            _mapPinLayer.Style      = null; // We don't want a global style for this layer

            _mapDrawableLayer.DataSource = new ObservableCollectionProvider <Drawable>(_drawable);
            _mapDrawableLayer.Style      = null; // We don't want a global style for this layer
        }