internal async Task MoveNearbyCircleToUser() { if (OwnedBy != SearchSection.Nearby) { return; } if (_tokenSource != null && !_tokenSource.IsCancellationRequested) { _tokenSource.Cancel(); } _tokenSource = new CancellationTokenSource(); IsOverviewLoading = true; GenericResult <Geoposition> result = await _geolocation.GetCurrentLocationAsync(); if (result.HasResult) { MapCircles.Clear(); _messenger.Send(new MessageTypes.CenterMapOnGeoposition(result.Result.Coordinate.Point.Position)); MapCircles.Add(new ColoredGeocircle(GeoHelper.GetGeocirclePoints(result.Result.Coordinate.Point, GeocircleRadiusMeters, GeocircleNumberOfPoints), result.Result.Coordinate.Point.Position)); await UpdateNearbyPlaces(new Geocircle(result.Result.Coordinate.Point.Position, GeocircleRadiusMeters), _tokenSource.Token); } IsOverviewLoading = false; }
/// <summary> /// Takes a list of places, and resolves NameOnly or UserLocation places /// into a usable Place with lat/lon coordinates. Leaves other Place types alone. /// </summary> /// <param name="places">List of places to resolve.</param> /// <param name="token"></param> /// <returns></returns> private async Task <IEnumerable <PlaceResolutionResult> > ResolvePlaces(List <IPlace> places, CancellationToken token) { List <Task <PlaceResolutionResult> > getAddressTasks = new List <Task <PlaceResolutionResult> >(); foreach (IPlace placeToFind in places) { if (placeToFind.Type == PlaceType.NameOnly) { Task <ApiResult <GeocodingResponse> > task = _networkService.SearchAddressAsync(placeToFind.Name, token); getAddressTasks.Add(task.ContinueWith(resp => { if (resp.Result.IsFailure) { FailureReason reason = resp.Result.Failure == null ? FailureReason.Unspecified : resp.Result.Failure.Reason; return(new PlaceResolutionResult(placeToFind, reason)); } if (resp.Result.Result.Features[0].Properties.Confidence <= Constants.SearchResultsMinimumConfidence) { FailureReason reason = FailureReason.NoResults; return(new PlaceResolutionResult(placeToFind, reason)); } IPlace foundPlace = new Place { Lon = (float)task.Result.Result.Features[0].Geometry.Coordinates[0], Lat = (float)task.Result.Result.Features[0].Geometry.Coordinates[1], Name = task.Result.Result.Features[0].Properties.Name, Type = PlaceType.Address, StringId = task.Result.Result.Features[0].Properties.Id, Confidence = task.Result.Result.Features[0].Properties.Confidence }; return(new PlaceResolutionResult(foundPlace)); })); } else if (placeToFind.Type == PlaceType.UserCurrentLocation) { Task <GenericResult <Geoposition> > task = _geolocationService.GetCurrentLocationAsync(); getAddressTasks.Add(task.ContinueWith <PlaceResolutionResult>(resp => { if (resp.Result.IsFailure) { return(new PlaceResolutionResult(placeToFind, resp.Result.Failure.Reason)); } if (resp.Result.Result.Coordinate?.Point?.Position == null) { return(new PlaceResolutionResult(placeToFind, FailureReason.NoResults)); } var loc = resp.Result.Result.Coordinate.Point.Position; IPlace foundPlace = new Place { Lon = (float)loc.Longitude, Lat = (float)loc.Latitude, Name = AppResources.SuggestBoxHeader_MyLocation, Type = PlaceType.UserCurrentLocation }; return(new PlaceResolutionResult(foundPlace)); })); } else { // TODO: Rework this to not require synchronous code to be wrapped in a Task. getAddressTasks.Add(Task.FromResult(new PlaceResolutionResult(placeToFind))); } } return(await Task.WhenAll(getAddressTasks)); }