Ejemplo n.º 1
0
        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));
            }

            try
            {
                Android.Gms.Location.LocationServices.FusedLocationApi.RequestLocationUpdates(mGoogleApiClient, mLocationRequest, GeofenceLocationListener.SharedInstance);
            }
            catch (System.Exception e)
            {
                // Do not crash the app if permissions are disabled on Android Marshmallow
                System.Diagnostics.Debug.WriteLine(e.Message);
                CrossGeofence.GeofenceListener.OnError(e.Message);
            }
        }
 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));
     }
     
     try 
     {
         Android.Gms.Location.LocationServices.FusedLocationApi.RequestLocationUpdates(mGoogleApiClient, mLocationRequest, GeofenceLocationListener.SharedInstance);
     }
     catch(System.Exception e)
     {
         // Do not crash the app if permissions are disabled on Android Marshmallow
         System.Diagnostics.Debug.WriteLine(e.Message);
         CrossGeofence.GeofenceListener.OnError(e.Message);
     }
 }