public void StartTracking()
 {
     if (_geo.IsGeolocationAvailable && !_geo.IsListening)
     {
         _geo.StartListening(30000, 0, false);
     }
 }
示例#2
0
        public void AddListener(EventHandler <PositionEventArgs> listener)
        {
            lock (_locker)
            {
                if (_locator == null)
                {
                    throw new Exception("Locator has not yet been bound to a platform-specific implementation.");
                }

                if (ListeningForChanges)
                {
                    _locator.StopListening();
                }

                PositionChanged += listener;

                _locator.StartListening(_minimumTimeHintMS, _desiredAccuracyMeters, true);

                SensusServiceHelper.Get().Logger.Log("GPS receiver is now listening for changes.", LoggingLevel.Normal, GetType());
            }
        }
        protected override void OnResume()
        {
            base.OnResume();

            // Get a handle on the map element
            _mapFragment = FragmentManager.FindFragmentById(Resource.Id.map) as MapFragment;
            _map         = _mapFragment.Map;

            _map.UiSettings.MyLocationButtonEnabled = true;
            _map.MyLocationEnabled = true;

            _locator = new Geolocator(this)
            {
                DesiredAccuracy = 1000
            };

            _locator.PositionChanged += OnLocationChanged;
            _locator.StartListening(2000, 1);
        }
示例#4
0
        public void StartTracking()
        {
            Setup();

            CurrentLocation.IsEnabled = true;

            if (IsTracking)
            {
                return;
            }

            if (!_geolocator.IsListening)
            {
                _geolocator.StartListening(5000, 1, true);
            }

            IsTracking = true;
            _fire();
        }
        public async Task <LocationCoordinates> GetCurrentLocation()
        {
            EventHandler <PositionEventArgs> handler = null;
            var result = new LocationCoordinates();
            TaskCompletionSource <LocationCoordinates> tcs = new TaskCompletionSource <LocationCoordinates>();
            Geolocator locator = new Geolocator {
                DesiredAccuracy = 50
            };

            try
            {
                if (!locator.IsListening)
                {
                    locator.StartListening(10, 100);
                }

                handler = (object sender, PositionEventArgs e) =>
                {
                    result.Status            = e.Position.Timestamp;
                    result.Latitude          = e.Position.Latitude;
                    result.Longitude         = e.Position.Longitude;
                    locator.PositionChanged -= handler;
                    tcs.SetResult(result);
                };
                locator.PositionChanged += handler;

                await locator.GetPositionAsync(timeout : 10000).ContinueWith(
                    t =>
                {
                });
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException.GetType().ToString() == "Xamarin.Geolocation.GeolocationException")
                {
                    tcs.SetException(ex);
                }
            }

            return(tcs.Task.Result);
        }
示例#6
0
        public void BeginTrackingLocation()
#endif
        {
            Init();

#if __ANDROID__
            geolocator = new Geolocator(context)
            {
                DesiredAccuracy = 1
            };
#else
            geolocator = new Geolocator()
            {
                DesiredAccuracy = 1
            };
#endif

            startTime = DateTime.Now;

            geolocator.PositionChanged += delegate(object sender, PositionEventArgs e) {
                if (e.Position.Accuracy == 0.0f || e.Position.Accuracy > maxHorizontalAccuracy)
                {
                    return;
                }

                positionHistory.Add(e.Position);

                if (previousPosition == null)
                {
                    previousDistanceTime = startTime;
                }

                if (positionHistory.Count > positionHistorySize)
                {
                    positionHistory.RemoveAt(0);
                }

                var canUpdateStats = positionHistory.Count >= minPositionsNeededToUpdateStats;

                if (e.Position.Timestamp - previousDistanceTime <= statsCalculationInterval)
                {
                    return;
                }

                previousDistanceTime = e.Position.Timestamp;

                Position bestPosition = null;
                var      bestAccuracy = maxHorizontalAccuracy;

                foreach (var position in positionHistory)
                {
                    if (DateTimeOffset.Now - position.Timestamp <= validLocationHistoryDeltaInterval && position.Accuracy < bestAccuracy && position != previousPosition)
                    {
                        bestAccuracy = position.Accuracy;
                        bestPosition = position;
                    }
                }

                if (bestPosition == null)
                {
                    bestPosition = e.Position;
                }

                if (canUpdateStats)
                {
                    if (previousPosition != null)
                    {
                        CurrentDistance += DistanceInMeters(bestPosition, previousPosition);

                        totalAveragePaceHistory.Add(bestPosition.Speed);
                        CurrentPace = bestPosition.Speed;
                    }
                }

                routePositions.Add(bestPosition);
                positionCounter++;

                // Only insert every fifth point to minimize the amount of data being stored.
                if (totalPositionHistory.Count == 0 || positionCounter % 5 == 0)
                {
                    totalPositionHistory.Add(new RunPosition {
                        Speed               = bestPosition.Speed,
                        Latitude            = bestPosition.Latitude,
                        Longitude           = bestPosition.Longitude,
                        PositionCaptureTime = bestPosition.Timestamp.DateTime
                    });
                }

                previousPosition = bestPosition;
            };

            geolocator.PositionError += delegate(object sender, PositionErrorEventArgs e) {
                Console.WriteLine("Location Manager Failed: " + e.Error);
            };

            geolocator.StartListening(minTime: 1000, minDistance: 1, includeHeading: false);
        }