/// <summary> /// Loads the saved location data on first navigation, and /// attaches a Geolocator.StatusChanged event handler. /// </summary> protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); if (e.NavigationMode == NavigationMode.New) { // Load location data from storage if it exists; // otherwise, load sample location data. var locations = await LocationDataStore.GetLocationDataAsync(); if (locations.Count == 0) { locations = await LocationDataStore.GetSampleLocationDataAsync(); } foreach (var location in locations) { this.Locations.Add(location); } // Start handling Geolocator and network status changes after loading the data // so that the view doesn't get refreshed before there is something to show. LocationHelper.Geolocator.StatusChanged += Geolocator_StatusChanged; NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged; } }
/// <summary> /// Handles delete button clicks to remove the selected /// location from the Locations collection. /// </summary> private async void DeleteLocation_Click(object sender, RoutedEventArgs e) { var location = this.GetLocation(sender as Button); int index = this.Locations.IndexOf(location); this.Locations.Remove(location); await LocationDataStore.SaveLocationDataAsync(this.Locations); }
/// <summary> /// Handles clicks to the Track button to flag the selected location for monitoring /// by a background task that periodically checks traffic and sends a notification /// whenever traffic adds 10 minutes or more to the travel time. /// </summary> private async void TrackButton_Click(object sender, RoutedEventArgs e) { var button = sender as ToggleButton; var location = this.GetLocation(button); location.IsMonitored = button.IsChecked.Value; this.UpdateTrafficMonitor(button.IsChecked.Value); await LocationDataStore.SaveLocationDataAsync(this.Locations); }
/// <summary> /// Applies the changes represented by the specified LocationData to /// the cached location in edit, saves the changes to the file system, /// and updates the user interface to account for the changes. /// </summary> private async Task SaveAsync(LocationData workingCopy) { Flyout.GetAttachedFlyout(this.GetTemplateRootForLocation(this.locationInEdit)).Hide(); this.isNewLocationInEdit = false; this.isExistingLocationBeingRepositioned = false; bool isAddressNew = workingCopy.Address != this.locationInEdit.Address; bool areCoordinatesNew = !workingCopy.Position.Equals(this.locationInEdit.Position); // If just the address OR just the coordinates are new, // clear the other value so that it can be updated. if (isAddressNew ^ areCoordinatesNew) { if (isAddressNew) { workingCopy.Position = new BasicGeoposition(); } if (areCoordinatesNew) { workingCopy.Address = string.Empty; } } // If the address, the coordinates, or both have changed, clear the travel // info and the route so that it doesn't reflect the old position. if (isAddressNew || areCoordinatesNew) { workingCopy.ClearTravelInfo(); this.InputMap.Routes.Clear(); } this.locationInEdit.Copy(workingCopy); var currentLocation = await this.GetCurrentLocationAsync(); if (currentLocation != null) { if (isAddressNew ^ areCoordinatesNew) { await LocationHelper.TryUpdateMissingLocationInfoAsync(this.locationInEdit, currentLocation); } } await LocationDataStore.SaveLocationDataAsync(this.Locations); if (currentLocation != null) { bool isNetworkAvailable = await this.TryUpdateLocationsTravelInfoAsync(this.Locations, currentLocation); if (isNetworkAvailable) { this.InputMap.Routes.Add(new MapRouteView(this.locationInEdit.FastestRoute)); } } }
public ItemsViewModel() { Title = "Kortlar"; Items = new ObservableCollection <Location>(); MapItems = new ObservableCollection <Location>(); LoadItemsCommand = new Command(async() => await ExecuteLoadItemsCommand()); MessagingCenter.Subscribe <NewItemPage, Location>(this, "AddItem", async(obj, item) => { var newItem = item as Location; Items.Add(newItem); MapItems.Add(newItem); await LocationDataStore.AddItemAsync(newItem); }); }
private void Geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args) { var _ = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { if (args.Position.Coordinate.Accuracy < 55) { var item = new SimpleGeoData { Position = args.Position.Coordinate.Point.Position }; App.userLocData.Add(item); await LocationDataStore.InsertLocationDataAsync(item); } }); }
/// <summary> /// Loads the saved location data on first navigation, and /// attaches a Geolocator.StatusChanged event handler. /// </summary> protected override async void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); if (e.NavigationMode == NavigationMode.New) { App.userLocData = await LocationDataStore.GetBasicGeopositionAsync(); } // Start handling Geolocator and network status changes after loading the data // so that the view doesn't get refreshed before there is something to show. LocationHelper.Geolocator.StatusChanged += Geolocator_StatusChanged; NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged; LocationHelper.Geolocator.PositionChanged += Geolocator_PositionChanged; StartLocationExtensionSession(); }
async Task ExecuteLoadItemsCommand() { if (IsBusy) { return; } IsBusy = true; try { Items.Clear(); var items = await LocationDataStore.GetItemsAsync(true); foreach (var item in items) { Items.Add(item); } foreach (var locationItem in Items) { try { MapItems.Add(locationItem); } catch (Exception ex) { continue; } } } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsBusy = false; } }