async Task <bool> CheckLocationAvailability(Android.Gms.Location.LocationRequest locationRequest)
        {
            // Build a new request with the given location request
            var locationSettingsRequest = new Android.Gms.Location.LocationSettingsRequest.Builder()
                                          .AddLocationRequest(locationRequest)
                                          .Build();


            //TODO: async issues
            // Ask the Settings API if we can fulfill this request
            var locationSettingsResult =
                //await
                Android.Gms.Location.LocationServices.SettingsApi
                //.CheckLocationSettingsAsync(googleApiClient, locationSettingsRequest);
                .CheckLocationSettings(googleApiClient, locationSettingsRequest);


            // If false, we might be able to resolve it by showing the location settings
            // to the user and allowing them to change the settings

            /*
             * if (!locationSettingsResult.Status.IsSuccess)
             * {
             *
             *      if (locationSettingsResult.Status.StatusCode == Android.Gms.Location.LocationSettingsStatusCodes.ResolutionRequired)
             *              locationSettingsResult.Status.StartResolutionForResult(this, 101);
             *      else
             *              Toast.MakeText(this, "Location Services Not Available for the given request.", ToastLength.Long).Show();
             *
             *      return false;
             * }
             */

            return(true);
        }
Пример #2
0
        void CreateLocationRequest()
        {
            locationRequest = new Android.Gms.Location.LocationRequest();
            locationRequest.SetInterval(UPDATE_INTERVAL);
            locationRequest.SetFastestInterval(FASTEST_INTERVAL);
            locationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityHighAccuracy);
            locationRequest.SetSmallestDisplacement(DISPLACEMENT);
            locationClient                = Android.Gms.Location.LocationServices.GetFusedLocationProviderClient(this);
            mLocationCallback             = new LocationCallbackHelper();
            mLocationCallback.MyLocation += MLocationCallback_MyLocation;

            Logger.Log("Location mamager initialized. ", "locationProvider");
        }
        internal void StartLocationUpdates()
        {
            Android.Gms.Location.LocationRequest mLocationRequest = new Android.Gms.Location.LocationRequest();
            mLocationRequest.SetInterval(CrossGeofence.LocationUpdatesInterval == 0 ? 30000 : CrossGeofence.LocationUpdatesInterval);
            mLocationRequest.SetFastestInterval(CrossGeofence.FastestLocationUpdatesInterval == 0 ? 5000 : CrossGeofence.FastestLocationUpdatesInterval);
            string priorityType = "Balanced Power";

            switch (CrossGeofence.GeofencePriority)
            {
            case GeofencePriority.HighAccuracy:
                priorityType = "High Accuracy";
                mLocationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityHighAccuracy);
                break;

            case GeofencePriority.LowAccuracy:
                priorityType = "Low Accuracy";
                mLocationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityLowPower);
                break;

            case GeofencePriority.LowestAccuracy:
                priorityType = "Lowest Accuracy";
                mLocationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityNoPower);
                break;

            case GeofencePriority.MediumAccuracy:
            case GeofencePriority.AcceptableAccuracy:
            default:
                mLocationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityBalancedPowerAccuracy);
                break;
            }

            System.Diagnostics.Debug.WriteLine(string.Format("{0} - {1}: {2}", CrossGeofence.Id, "Priority set to", priorityType));
            //(Regions.Count == 0) ? (CrossGeofence.SmallestDisplacement==0?50 :CrossGeofence.SmallestDisplacement): Regions.Min(s => (float)s.Value.Radius)
            if (CrossGeofence.SmallestDisplacement > 0)
            {
                mLocationRequest.SetSmallestDisplacement(CrossGeofence.SmallestDisplacement);
                System.Diagnostics.Debug.WriteLine(string.Format("{0} - {1}: {2} meters", CrossGeofence.Id, "Location smallest displacement set to", CrossGeofence.SmallestDisplacement));
            }

            Android.Gms.Location.LocationServices.FusedLocationApi.RequestLocationUpdates(mGoogleApiClient, mLocationRequest, GeofenceLocationListener.SharedInstance);
        }
        async Task RequestLocationUpdates()
        {
            // Describe our location request
            var locationRequest = new Android.Gms.Location.LocationRequest()
                                  .SetInterval(10000)
                                  .SetFastestInterval(1000)
                                  .SetPriority(Android.Gms.Location.LocationRequest.PriorityHighAccuracy);

            // Check to see if we can request updates first
            if (await CheckLocationAvailability(locationRequest))
            {
                // Request updates
                //await
                Android.Gms.Location.LocationServices.FusedLocationApi.RequestLocationUpdates
                (
                    googleApiClient,
                    locationRequest,
                    this
                );
            }
        }
        protected override async void OnResume()
        {
            base.OnResume();

            // Subscribe for location updates
            locationCallback.LocationResult += LocationCallback_LocationResult;

            // Build our location request setting interval, priority, etc
            var locationRequest = new Android.Gms.Location.LocationRequest()
                                  .SetInterval(10000)
                                  .SetFastestInterval(1000)
                                  .SetPriority(Android.Gms.Location.LocationRequest.PriorityHighAccuracy);

            // Start receiving location updates
            await fusedClient.RequestLocationUpdatesAsync(locationRequest, locationCallback);

            // Get the last known location so we can show the user something immediately
            var lastLocation = await fusedClient.GetLastLocationAsync();

            if (lastLocation != null)
            {
                textLastLocation.Text = DescribeLocation(lastLocation);
            }
        }
    internal void StartLocationUpdates()
    {
        Android.Gms.Location.LocationRequest mLocationRequest = new Android.Gms.Location.LocationRequest();
        mLocationRequest.SetInterval(CrossGeofence.LocationUpdatesInterval == 0 ? 30000 : CrossGeofence.LocationUpdatesInterval);
        mLocationRequest.SetFastestInterval(CrossGeofence.FastestLocationUpdatesInterval == 0 ? 5000 : CrossGeofence.FastestLocationUpdatesInterval);
        string priorityType = "Balanced Power";
        switch (CrossGeofence.GeofencePriority)
        {
            case GeofencePriority.HighAccuracy:
                priorityType = "High Accuracy";
                mLocationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityHighAccuracy);
                break;
            case GeofencePriority.LowAccuracy:
                priorityType = "Low Accuracy";
                mLocationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityLowPower);
                break;
            case GeofencePriority.LowestAccuracy:
                priorityType = "Lowest Accuracy";
                mLocationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityNoPower);
                break;
            case GeofencePriority.MediumAccuracy:
            case GeofencePriority.AcceptableAccuracy:
            default:
                mLocationRequest.SetPriority(Android.Gms.Location.LocationRequest.PriorityBalancedPowerAccuracy);
                break;
        }
    
        System.Diagnostics.Debug.WriteLine(string.Format("{0} - {1}: {2}", CrossGeofence.Id, "Priority set to", priorityType));
        //(Regions.Count == 0) ? (CrossGeofence.SmallestDisplacement==0?50 :CrossGeofence.SmallestDisplacement): Regions.Min(s => (float)s.Value.Radius)
        if (CrossGeofence.SmallestDisplacement > 0)
        {
            mLocationRequest.SetSmallestDisplacement(CrossGeofence.SmallestDisplacement);
            System.Diagnostics.Debug.WriteLine(string.Format("{0} - {1}: {2} meters", CrossGeofence.Id, "Location smallest displacement set to", CrossGeofence.SmallestDisplacement));
        }
 
        Android.Gms.Location.LocationServices.FusedLocationApi.RequestLocationUpdates(mGoogleApiClient, mLocationRequest, GeofenceLocationListener.SharedInstance);
    }