Ejemplo n.º 1
0
		public void StopListening()
		{
			if (_listener == null)
				return;

			_listener.PositionChanged -= OnPositionChanged;
			_listener.PositionError -= OnPositionError;
			_listener.OrientationChanged -= OnHeadingChanged;
			for (int i = 0; i < _providers.Length; ++i)
			{
				_locManager.RemoveUpdates(_listener);
			}

			_listener = null;

			// Save last known position for later
			if (this._lastPosition != null)
			{
				Settings.Current.AddOrUpdateValue(Settings.LastKnownPositionLatitudeKey, this._lastPosition.Latitude);
				Settings.Current.AddOrUpdateValue(Settings.LastKnownPositionLongitudeKey, this._lastPosition.Longitude);
				Settings.Current.AddOrUpdateValue(Settings.LastKnownPositionAltitudeKey, this._lastPosition.Altitude);
			}
		}
Ejemplo n.º 2
0
		public void StartListening(uint minTime, double minDistance, bool includeHeading)
		{
			// Get last known position from settings
			_lastPosition = _lastPosition ?? new Position();

			_lastPosition.Latitude = Settings.Current.GetValueOrDefault<double>(Settings.LastKnownPositionLatitudeKey);
			_lastPosition.Longitude = Settings.Current.GetValueOrDefault<double>(Settings.LastKnownPositionLongitudeKey);
			_lastPosition.Altitude = Settings.Current.GetValueOrDefault<double>(Settings.LastKnownPositionAltitudeKey);

			if (minTime < 0)
				throw new ArgumentOutOfRangeException("minTime");
			if (minDistance < 0)
				throw new ArgumentOutOfRangeException("minDistance");

			if (IsListening)
			{
				return;
			}

			_listener = new GeolocationListener(_locManager, TimeSpan.FromMilliseconds(minTime), _providers);
			_listener.PositionChanged += OnPositionChanged;
			_listener.PositionError += OnPositionError;
			Looper looper = Looper.MyLooper() ?? Looper.MainLooper;
			for (int i = 0; i < _providers.Length; ++i)
				_locManager.RequestLocationUpdates(_providers[i], minTime, (float) minDistance, _listener, looper);

			// Add orientation event listener
			if (includeHeading) {
				_listener.OrientationChanged += OnHeadingChanged;

				_orientationSensor = _sensorManager.GetDefaultSensor(SensorType.Orientation);
				_accelerometerSensor = _sensorManager.GetDefaultSensor(SensorType.Accelerometer);

				_sensorManager.RegisterListener(_listener, _orientationSensor, SensorDelay.Ui);
				_sensorManager.RegisterListener(_listener, _accelerometerSensor, SensorDelay.Ui);
			}
		}