コード例 #1
0
        bool GlobalLocationServicesEnabled()
        {
            var locationServicesEnabled = CLLocationManager.LocationServicesEnabled;

            SimpleLocationLogger.Log("Location services enabled = " + locationServicesEnabled);
            return(locationServicesEnabled);
        }
コード例 #2
0
        public void OnResult(Java.Lang.Object result)
        {
            var locationSettingsResult = result as LocationSettingsResult;

            var status = locationSettingsResult.Status;

            switch (status.StatusCode)
            {
            case CommonStatusCodes.Success:
                SimpleLocationLogger.Log("All location settings are satisfied");
                StartUpdates();
                break;

            case CommonStatusCodes.ResolutionRequired:
                SimpleLocationLogger.Log("Location settings are not satisfied");
                try {
                    if (context is Activity)
                    {
                        status.StartResolutionForResult(context as Activity, requestCheckSettings);
                    }
                    // Handle result in OnActivityResult of your Activity by calling HandleResolutionResultForLocationSettings
                } catch (IntentSender.SendIntentException) {
                    SimpleLocationLogger.Log("PendingIntent unable to execute request");
                }
                break;

            case LocationSettingsStatusCodes.SettingsChangeUnavailable:
                SimpleLocationLogger.Log("Location settings are inadequate and cannot be fixed here");
                break;
            }
        }
コード例 #3
0
        bool IsAuthorized()
        {
            var status = CLLocationManager.Status;

            SimpleLocationLogger.Log("Authorization status = " + status);
            return(status == CLAuthorizationStatus.AuthorizedAlways || status == CLAuthorizationStatus.AuthorizedWhenInUse);
        }
コード例 #4
0
        public void OnConnectionFailed(ConnectionResult result)
        {
            if (resolvingError)
            {
                return; // Already attempting to resolve an error.
            }
            if (!(context is Activity))
            {
                SimpleLocationLogger.Log("Connection failed. Error: " + result.ErrorCode);
                return;
            }

            if (result.HasResolution)
            {
                try {
                    resolvingError = true;
                    result.StartResolutionForResult(context as Activity, requestResolveError);
                } catch (Exception e) {
                    SimpleLocationLogger.Log("Connection failed. Error: " + e.Message);
                    googleApiClient.Connect(); // There was an error with the resolution intent. Try again.
                }
            }
            else
            {
                var dialog = GoogleApiAvailability.Instance.GetErrorDialog(context as Activity, result.ErrorCode, requestResolveError);
                dialog.DismissEvent += (sender, e) => resolvingError = false;
                dialog.Show();

                resolvingError = true;
            }
        }
コード例 #5
0
        void InitLocationManager()
        {
            if (locationManager == null)
            {
                locationManager = new CLLocationManager();
                locationManager.AuthorizationChanged += (sender, e) => {
                    AuthorizationChanged(sender, e);
                    if (isInitializing)   // Necessary because AuthorizationChanged get's called even on App start
                    {
                        isInitializing = false;
                        return;
                    }

                    SimpleLocationLogger.Log("Authorization changed to " + e.Status);

                    if (AppHasLocationPermission())
                    {
                        if (shouldBeUpdatingLocation)
                        {
                            TryToStartUpdates();
                        }
                    }
                    else
                    {
                        StopLocationUpdates();
                        TriggerAppPermissionDialog();
                    }
                };

                locationManager.LocationsUpdated += (sender, e) => {
                    var location = e.Locations.Last();
                    LastLocation           = new Location(location.Coordinate.Latitude, location.Coordinate.Longitude);
                    LastLocation.Direction = location.Course >= 0 ? location.Course : 0;
                    LastLocation.Speed     = location.Speed >= 0 ? location.Speed : 0;
                    if (location.VerticalAccuracy < 0 || location.HorizontalAccuracy < 0) // accurary can be invalid
                    {
                        LastLocation.Accuracy = -1;
                    }
                    else
                    {
                        LastLocation.Accuracy = Math.Max(location.VerticalAccuracy, location.HorizontalAccuracy);
                    }
                    LocationUpdated();
                };

                if (locationManager.Location != null)
                {
                    LastLocation = new Location(locationManager.Location.Coordinate.Latitude, locationManager.Location.Coordinate.Longitude);
                }
            }
        }
コード例 #6
0
        void StartUpdates()
        {
            var location = LocationServices.FusedLocationApi.GetLastLocation(googleApiClient);

            if (location != null)
            {
                LastLocation = new Location(location.Latitude, location.Longitude);
            }

            try {
                LocationServices.FusedLocationApi.RequestLocationUpdates(googleApiClient, CreateLocationRequest(), this);
            } catch (Exception e) {
                SimpleLocationLogger.Log("Requesting location updates failed. Message: " + e.Message);
                SimpleLocationLogger.Log("Stack trace: " + System.Environment.StackTrace);
            }
            LocationUpdatesStarted();
        }
コード例 #7
0
        public void HandleResolutionResultForLocationSettings(int requestCode, Result resultCode)
        {
            switch (requestCode)
            {
            case requestCheckSettings:
                switch (resultCode)
                {
                case Result.Ok:
                    SimpleLocationLogger.Log("User agreed to make required location settings changes");
                    StartUpdates();
                    break;

                case Result.Canceled:
                    SimpleLocationLogger.Log("User chose not to make required location settings changes");
                    if (HowOftenShowUseLocationDialog == ShowUseLocationDialog.Once)
                    {
                        showUseLocationDialog = false;
                    }
                    break;
                }
                break;
            }
        }
コード例 #8
0
 public void OnConnectionSuspended(int cause)
 {
     SimpleLocationLogger.Log("Connection suspended. Cause: " + cause);
 }