Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StartPage"/> class.
        /// </summary>
        public StartPage()
        {
            InitializeComponent();
            InitializeBasemapSwitcher();

            PictureMarkerSymbol endMapPin   = CreateMapPin("end.png");
            PictureMarkerSymbol startMapPin = CreateMapPin("start.png");

            var geocodeViewModel     = Resources["GeocodeViewModel"] as GeocodeViewModel;
            var fromGeocodeViewModel = Resources["FromGeocodeViewModel"] as GeocodeViewModel;
            var toGeocodeViewModel   = Resources["ToGeocodeViewModel"] as GeocodeViewModel;

            _routeViewModel = Resources["RouteViewModel"] as RouteViewModel;

            geocodeViewModel.PropertyChanged += (o, e) =>
            {
                switch (e.PropertyName)
                {
                case nameof(GeocodeViewModel.Place):
                {
                    var graphicsOverlay = MapView.GraphicsOverlays["PlacesOverlay"];
                    graphicsOverlay?.Graphics.Clear();

                    GeocodeResult place = geocodeViewModel.Place;
                    if (place == null)
                    {
                        return;
                    }

                    var graphic = new Graphic(geocodeViewModel.Place.DisplayLocation, endMapPin);
                    graphicsOverlay?.Graphics.Add(graphic);

                    break;
                }
                }
            };

            fromGeocodeViewModel.PropertyChanged += (o, e) =>
            {
                switch (e.PropertyName)
                {
                case nameof(GeocodeViewModel.Place):
                {
                    _routeViewModel.FromPlace = fromGeocodeViewModel.Place;
                    break;
                }
                }
            };

            toGeocodeViewModel.PropertyChanged += (o, e) =>
            {
                switch (e.PropertyName)
                {
                case nameof(GeocodeViewModel.Place):
                {
                    _routeViewModel.ToPlace = toGeocodeViewModel.Place;
                    break;
                }
                }
            };

            _routeViewModel.PropertyChanged += (s, e) =>
            {
                switch (e.PropertyName)
                {
                case nameof(RouteViewModel.Route):
                {
                    var graphicsOverlay = MapView.GraphicsOverlays["RouteOverlay"];

                    // clear existing graphics
                    graphicsOverlay?.Graphics?.Clear();

                    if (_routeViewModel.FromPlace == null || _routeViewModel.ToPlace == null ||
                        _routeViewModel.Route == null || graphicsOverlay == null)
                    {
                        return;
                    }

                    // Add route to map
                    var routeGraphic = new Graphic(_routeViewModel.Route.Routes.FirstOrDefault()?.RouteGeometry);
                    graphicsOverlay?.Graphics.Add(routeGraphic);

                    // Add start and end locations to the map
                    var fromGraphic = new Graphic(_routeViewModel.FromPlace.RouteLocation, startMapPin);
                    var toGraphic   = new Graphic(_routeViewModel.ToPlace.RouteLocation, endMapPin);
                    graphicsOverlay?.Graphics.Add(fromGraphic);
                    graphicsOverlay?.Graphics.Add(toGraphic);

                    break;
                }
                }
            };

            // start location services
            var mapViewModel = Resources["MapViewModel"] as MapViewModel;

            MapView.LocationDisplay.DataSource  = mapViewModel.LocationDataSource;
            MapView.LocationDisplay.AutoPanMode = LocationDisplayAutoPanMode.Recenter;
            MapView.LocationDisplay.IsEnabled   = true;

#if __IOS__
            // This is necessary because on iOS the SearchBar doesn't get unfocused automatically when a geocode result is selected
            SearchSuggestionsList.ItemSelected += (s, e) =>
            {
                AddressSearchBar.Unfocus();
            };

            FromLocationSuggestionsList.ItemSelected += (s, e) =>
            {
                FromLocationTextBox.Unfocus();
            };

            ToLocationSuggestionsList.ItemSelected += (s, e) =>
            {
                ToLocationTextBox.Unfocus();
            };
#endif
        }
Esempio n. 2
0
        /// <summary>
        /// Performs initialization
        /// </summary>
        private async void Initialize()
        {
            try
            {
                var routeStyle = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.FromArgb(0x00, 0x79, 0xc1), 5);
                RouteRenderer.Symbol = routeStyle;

                PictureMarkerSymbol endMapPin = await CreateMapPin("end.png");

                PictureMarkerSymbol startMapPin = await CreateMapPin("start.png");

                var geocodeViewModel     = Resources["GeocodeViewModel"] as GeocodeViewModel;
                var fromGeocodeViewModel = Resources["FromGeocodeViewModel"] as GeocodeViewModel;
                var toGeocodeViewModel   = Resources["ToGeocodeViewModel"] as GeocodeViewModel;
                _routeViewModel     = Resources["RouteViewModel"] as RouteViewModel;
                _basemapViewModel   = Resources["BasemapsViewModel"] as BasemapsViewModel;
                _userItemsViewModel = Resources["UserItemsViewModel"] as UserItemsViewModel;

                geocodeViewModel.PropertyChanged += (o, e) =>
                {
                    switch (e.PropertyName)
                    {
                    case nameof(GeocodeViewModel.Place):
                    {
                        var graphicsOverlay = MapView.GraphicsOverlays["PlacesOverlay"];
                        graphicsOverlay?.Graphics.Clear();

                        GeocodeResult place = geocodeViewModel.Place;
                        if (place == null)
                        {
                            return;
                        }

                        var graphic = new Graphic(geocodeViewModel.Place.DisplayLocation, endMapPin);
                        graphicsOverlay?.Graphics.Add(graphic);

                        break;
                    }
                    }
                };

                fromGeocodeViewModel.PropertyChanged += (o, e) =>
                {
                    switch (e.PropertyName)
                    {
                    case nameof(GeocodeViewModel.Place):
                    {
                        _routeViewModel.FromPlace = fromGeocodeViewModel.Place;
                        break;
                    }
                    }
                };

                toGeocodeViewModel.PropertyChanged += (o, e) =>
                {
                    switch (e.PropertyName)
                    {
                    case nameof(GeocodeViewModel.Place):
                    {
                        _routeViewModel.ToPlace = toGeocodeViewModel.Place;
                        break;
                    }
                    }
                };

                _routeViewModel.PropertyChanged += (s, e) =>
                {
                    switch (e.PropertyName)
                    {
                    case nameof(RouteViewModel.Route):
                    {
                        var graphicsOverlay = MapView.GraphicsOverlays["RouteOverlay"];

                        // clear existing graphics
                        graphicsOverlay?.Graphics?.Clear();

                        if (_routeViewModel.FromPlace == null || _routeViewModel.ToPlace == null ||
                            _routeViewModel.Route == null || graphicsOverlay == null)
                        {
                            return;
                        }

                        // Add route to map
                        var routeGraphic = new Graphic(_routeViewModel.Route.Routes.FirstOrDefault()?.RouteGeometry);
                        graphicsOverlay?.Graphics.Add(routeGraphic);

                        // Add start and end locations to the map
                        var fromGraphic = new Graphic(_routeViewModel.FromPlace.RouteLocation, startMapPin);
                        var toGraphic   = new Graphic(_routeViewModel.ToPlace.RouteLocation, endMapPin);
                        graphicsOverlay?.Graphics.Add(fromGraphic);
                        graphicsOverlay?.Graphics.Add(toGraphic);

                        break;
                    }
                    }
                };

                // start location services - only once MapView.LocationDisplay property is ready
                MapView.PropertyChanged += (o, e) =>
                {
                    if (e.PropertyName == nameof(MapView.LocationDisplay) && MapView.LocationDisplay != null)
                    {
                        var mapViewModel = Resources["MapViewModel"] as MapViewModel;
                        MapView.LocationDisplay.DataSource  = mapViewModel.LocationDataSource;
                        MapView.LocationDisplay.AutoPanMode = LocationDisplayAutoPanMode.Recenter;

#if __ANDROID__
                        MainActivity.Instance.AskForLocationPermission(MapView);
#else
                        MapView.LocationDisplay.IsEnabled = true;
#endif
                    }
                };

                // Change map when user selects a new basemap
                _basemapViewModel.PropertyChanged += (s, ea) =>
                {
                    switch (ea.PropertyName)
                    {
                    case nameof(BasemapsViewModel.SelectedBasemap):
                    {
                        // Set the viewpoint of the new map to be the same as the old map
                        // Otherwise map is being reset to the world view
                        var mapViewModel = Resources["MapViewModel"] as MapViewModel;

                        try
                        {
                            var currentViewpoint = MapView.GetCurrentViewpoint(ViewpointType.CenterAndScale);
                            mapViewModel.Map.Basemap = new Basemap(_basemapViewModel.SelectedBasemap);
                        }
                        catch (Exception ex)
                        {
                            mapViewModel.ErrorMessage = "Unable to change basemaps";
                            mapViewModel.StackTrace   = ex.ToString();
                        }

                        break;
                    }
                    }
                };

                // Change map when user selects a new user item
                _userItemsViewModel.PropertyChanged += async(s, ea) =>
                {
                    switch (ea.PropertyName)
                    {
                    case nameof(UserItemsViewModel.SelectedUserItem):
                    {
                        // Set the viewpoint of the new map to be the same as the old map
                        // Otherwise map is being reset to the world view
                        var mapViewModel     = Resources["MapViewModel"] as MapViewModel;
                        var currentViewpoint = MapView.GetCurrentViewpoint(ViewpointType.CenterAndScale);

                        try
                        {
                            var newMap = new Map(_userItemsViewModel.SelectedUserItem)
                            {
                                InitialViewpoint = currentViewpoint
                            };

                            //Load new map
                            await newMap.LoadAsync();

                            mapViewModel.Map = newMap;
                        }
                        catch (Exception ex)
                        {
                            mapViewModel.ErrorMessage = "Unable to change maps";
                            mapViewModel.StackTrace   = ex.ToString();
                        }

                        break;
                    }
                    }
                };


#if __IOS__
                // This is necessary because on iOS the SearchBar doesn't get unfocused automatically when a geocode result is selected
                SearchSuggestionsList.ItemSelected += (s, e) =>
                {
                    AddressSearchBar.Unfocus();
                };

                FromLocationSuggestionsList.ItemSelected += (s, e) =>
                {
                    FromLocationTextBox.Unfocus();
                };

                ToLocationSuggestionsList.ItemSelected += (s, e) =>
                {
                    ToLocationTextBox.Unfocus();
                };
#endif
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", ex.ToString(), "OK");
            }
        }