/// <summary> /// Updates the UI to account for the user's current position, if available, /// resetting the MapControl bounds and refreshing the travel info. /// </summary> /// <param name="isGeolocatorReady">false if the Geolocator is known to be unavailable; otherwise, true.</param> /// <returns></returns> private async Task ResetViewAsync(bool isGeolocatorReady = true) { LocationData currentLocation = null; if (isGeolocatorReady) { currentLocation = await GetCurrentLocationAsync(); } if (currentLocation != null) { if (MappedLocations.Count > 0 && MappedLocations[0].IsCurrentLocation) { MappedLocations.RemoveAt(0); } await LocationHelper.TryUpdateMissingLocationInfoAsync(currentLocation, "Current Location"); MappedLocations.Insert(0, new LocationData { Name = "Current Location", Position = currentLocation.Position, IsCurrentLocation = true }); RouteWaypoints.Insert(0, currentLocation); } // Set the current view of the map control. var positions = Locations.Select(loc => loc.Position).ToList(); if (currentLocation != null) { positions.Insert(0, currentLocation.Position); } if (positions.Count > 0) { bool isSuccessful = await SetViewBoundsAsync(positions); if (isSuccessful && positions.Count < 2) { MyMap.ZoomLevel = 15; } else if (!isSuccessful && positions.Count > 0) { MyMap.Center = new Geopoint(positions[0]); MyMap.ZoomLevel = 15; } } }
public MainViewModel() { Restaurants.CollectionChanged += async(s, e) => { if (e.NewItems != null) { foreach (Restaurant restaurant in e.NewItems) { MappedLocations.Add( new MappedLocation { Restaurant = restaurant, Index = Restaurants.IndexOf(restaurant) + 1, Position = new BasicGeoposition { Latitude = restaurant.Latitude, Longitude = restaurant.Longitude } }); if (restaurant.Distance == 0) { await SetDistanceAsync(restaurant); } } } if (e.OldItems != null) { foreach (Restaurant restaurant in e.OldItems) { MappedLocations.Remove(MappedLocations.First(location => location.Restaurant == restaurant)); } foreach (var location in MappedLocations) { location.Index = Restaurants.IndexOf(location.Restaurant) + 1; } } }; }