public SearchFilersPage() { Utils.SetupPage(this); BuildLayout(); locator = Xamarin.Forms.DependencyService.Get <IGeolocator>(); locator.PositionChanged += (o, arg) => { locator.StopListening(); if (arg.Position != null) { LocationFilter = new LocationFilter() { Lat = arg.Position.Latitude, Lon = arg.Position.Longitude, Radius = RADIUS }; LocationAddress = "Current location"; } }; locator.PositionError += (o, arg) => { locator.StopListening(); }; locator.StopListening(); locator.StartListening(0, 0); }
private void LocationClicked(View v) { locationEntry.Text = ""; LocationFilter = null; locator.StopListening(); locator.StartListening(0, 0); }
public void StopListening() { if (m_Locator != null && m_Locator.IsListening) { m_Locator.StopListening(); m_Locator.PositionChanged -= LocationManager_PositionChanged; } }
private void ListenToPosition(bool listening) { if (_location.IsListening) { try { _location.StopListening(); } catch { } } if (listening) { _location.PositionChanged += LocationService_PositionChanged; try { _location.StartListening(10000, 30); } catch { } } }
private void AutoPositionSwitch_Toggled(object sender, ToggledEventArgs e) { if (e.Value) { if (!_location.IsListening) { try { _location.StartListening(10000, 30); } catch { } } } else { try { _location.StopListening(); } catch { } } }
private async void LoadMap() { //map.Pins.Clear(); //map.RouteCoordinates.Clear(); //map.ShapeCoordinates.Clear(); //map.IsVisible = true; //map.MapPositionOption = CurrentRecordEdit.Position; //switch (CurrentRecordEdit.Position) //{ // case PositionInfo.PositionOption.Pin: // map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(CurrentRecordEdit.Latitude, CurrentRecordEdit.Longitude), new Distance(100))); // UpdatePin(); // break; // case PositionInfo.PositionOption.Line: // foreach (var position in CurrentRecordEdit.PositionList) // { // map.RouteCoordinates.Add(new Position(position.Latitude, position.Longitude)); // } // map.MoveToRegion(MapSpan.FromCenterAndRadius(GetCenterOfPositions(CurrentRecordEdit.PositionList), new Distance(GetDistanceFromCenter(CurrentRecordEdit.PositionList)))); // break; // case PositionInfo.PositionOption.Area: // foreach (var position in CurrentRecordEdit.PositionList) // { // map.ShapeCoordinates.Add(new Position(position.Latitude, position.Longitude)); // } // map.MoveToRegion(MapSpan.FromCenterAndRadius(GetCenterOfPositions(CurrentRecordEdit.PositionList), new Distance(GetDistanceFromCenter(CurrentRecordEdit.PositionList)))); // break; // default: // break; //} MapSpan span = MapSpan.FromCenterAndRadius(new Position(RecordEditViewModel.Latitude, RecordEditViewModel.Longitude), new Distance(75000)); map.MoveToRegion(span); try { var statusLocation = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location); if (statusLocation != PermissionStatus.Granted) { if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location)) { await Application.Current.MainPage.DisplayAlert("Benötige Standort-Berechtigung", "Zum automatischen Feststellen des Fundorts wird die Standort-Berechtigung benötigt.", "Okay"); } var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Location }); statusLocation = results[Permission.Location]; } if (statusLocation == PermissionStatus.Granted) { _location = DependencyService.Get <IGeolocator>(); } else if (statusLocation != PermissionStatus.Unknown) { await Application.Current.MainPage.DisplayAlert("Berechtigung verweigert", "Ohne Berechtigung kann der Standort nicht automatisch festgestellt werden.", "Okay"); } } catch { } _location = DependencyService.Get <IGeolocator>(); // LoadMap(); if (!RecordEditViewModel.ExistingRecord && _location != null && _location.IsGeolocationAvailable && _location.IsGeolocationEnabled) { try { _location.PositionChanged += LocationService_PositionChanged; } catch (Exception ex) { Debug.WriteLine(ex.Message); } if (_location.IsListening) { try { _location.StopListening(); } catch (Exception ex) { Debug.WriteLine(ex.Message); } } try { _location.StartListening(10000, 30); } catch (Exception ex) { Debug.WriteLine(ex.Message); } RecordEditViewModel.AutoPosition = true; AutoPositionSwitch.IsEnabled = true; } else { RecordEditViewModel.AutoPosition = false; AutoPositionSwitch.IsEnabled = false; } if (Device.RuntimePlatform == Device.Android) { MapHolder.Scrolled += OnScrolled; } }
private void Locator_PositionError(object sender, PositionErrorEventArgs e) { locator.StopListening(); }
/// <summary> /// Method that handles change in positions from the gps signal /// </summary> public void PositionChanged(object sender, PositionEventArgs e) { IsBusy = true; if (_locator != null) { _locator.GetPositionAsync(timeout: Definitions.MinIntervalTimeout, cancelToken: this._cancelSource.Token, includeHeading: false) .ContinueWith(t => { IsBusy = false; if (t.IsFaulted) { GpsNotAvailable(); //PositionStatus = ((GeolocationException)t.Exception.InnerException).Error.ToString(); } else if (t.IsCanceled) { PositionStatus = "Canceled"; } else { if (t.Result.Accuracy < 100) { GpsIsAvailable(); } else { GpsNotAvailable(); return; } if (_trackChanges) { if (_firstRun) { _positionLatitude = t.Result.Latitude; _positionLongitude = t.Result.Longitude; _firstRun = false; return; } var testDistance = Math.Round(GpsCalculator.Distance(_positionLatitude, _positionLongitude, t.Result.Latitude, t.Result.Longitude, 'K'), 3); // Check after resuming. if (_doTestCheck) { // If the user moved more than 200 meters. Resuming is not allowed. if (testDistance > 0.2) { _trackChanges = false; _pause = !_pause; _locator.StopListening(); PauseDistanceTooBig(testDistance); return; } _doTestCheck = false; } // Discard if distance is below 10 meters if (t.Result.Accuracy != null) { SetAccuracy(t.Result.Accuracy); if (testDistance < t.Result.Accuracy / 1000) { return; } } else { // default accuracy if (testDistance < 0.01) { return; } } SetAccuracy(t.Result.Accuracy); TraveledDistance = testDistance; _positionLatitude = t.Result.Latitude; _positionLongitude = t.Result.Longitude; PositionStatus = t.Result.Timestamp.ToString("HH:mm:ss"); Definitions.Route.GPSCoordinates.Add(new GPSCoordinate { Latitude = t.Result.Latitude.ToString("#.######", CultureInfo.InvariantCulture), Longitude = t.Result.Longitude.ToString("#.######", CultureInfo.InvariantCulture), }); } } }); } }