/// <summary>
        /// Stop listen to location update / gatherer device location.
        /// </summary>
        private void StopLocationUpdate(GeolocationStatus status)
        {
            if (LocationRetrieveTimer != null)
            {
                // Destroy the timer
                LocationRetrieveTimer.Dispose();
                LocationRetrieveTimer = null;
            }

            if (LocationManager != null)
            {
                // Stop listen to location changes
                LocationManager.StopUpdatingLocation();
                LocationManager.LocationsUpdated -= LocationManager_LocationsUpdated;

                // Remove the reference
                LocationManager = null;
            }

            // Make sure we only update the result if it is not completed yet
            if (LocationTaskCompletionSource != null &&
                !LocationTaskCompletionSource.Task.IsCompleted)
            {
                // Craft the location with all information we have whatever the location is acceptable or not
                var location = IsLastKnownLocationFresh() ?
                               LastKnownLocation.ToNewGeolocation(status) :
                               new Geolocation(status);

                // return the location
                LocationTaskCompletionSource.SetResult(location);
            }
        }
Пример #2
0
        /// <summary>
        /// Stop listen to location update / gatherer device location.
        /// </summary>
        private void StopLocationUpdate(GeolocationStatus status)
        {
            if (LocationRetrieveTimer != null)
            {
                // Destroy the timer
                LocationRetrieveTimer.Dispose();
                LocationRetrieveTimer = null;
            }

            if (FusedLocationClient != null)
            {
                // Stop listen to location changes
                if (FusedLocationCallback != null)
                {
                    FusedLocationCallback.LocationResult -= FusedLocationCallback_LocationResult;
                    FusedLocationClient.RemoveLocationUpdates(FusedLocationCallback);
                    FusedLocationCallback = null;
                }
                FusedLocationClient.Dispose();
                FusedLocationClient = null;
            }

            if (LocationManager != null)
            {
                // Stop listen to location changes
                LocationManager.RemoveUpdates(this);
                LocationManager.Dispose();
                LocationManager = null;
            }

            // Make sure we only update the result if it is not completed yet
            if (LocationTaskCompletionSource != null &&
                !LocationTaskCompletionSource.Task.IsCompleted)
            {
                // Craft the location with all information we have whatever the location is acceptable or not
                var location = IsLastKnownLocationFresh() ?
                               LastKnownLocation.ToNewGeolocation(status) :
                               new Geolocation(status);

                // return the location
                LocationTaskCompletionSource.SetResult(location);
            }
        }