Exemplo n.º 1
0
 void doOnLocationUpdated(Location location)
 {
     this.TimeUpdated = DateTime.Now;
     this.Location    = location;
     this.FailureInfo = "";
     if (this.Location != null)
     {
         LocationServicesStatus = LocationServicesStatusEnum.Ok;
     }
     this.fireEvents();
 }
Exemplo n.º 2
0
        void requestLocationAsync(bool askPermissionIfNecessary)
        {
            // is not authorized?
            if (CoreLocation.CLLocationManager.Status == CLAuthorizationStatus.Restricted ||
                CoreLocation.CLLocationManager.Status == CLAuthorizationStatus.Denied)
            {
                Task.Run(() =>
                {
                    Location = null; // location is not known
                    LocationServicesStatus = LocationServicesStatusEnum.NotAuthorized;
                    FailureInfo            = "Not authorized to use location services";
                    fireEvents();
                });
                return;
            }

            // initialize
            initLocationManager();

            // request permission
            if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0) == true &&
                CoreLocation.CLLocationManager.Status == CLAuthorizationStatus.NotDetermined)
            {
                if (askPermissionIfNecessary == false)
                {
                    return; // don't do anything if the caller doesn't want to ask for permission
                }
                locationManager.AuthorizationChanged += (s, e) =>
                {
                    if (e.Status == CLAuthorizationStatus.NotDetermined)
                    {
                        return; // in progress
                    }
                    if (e.Status == CLAuthorizationStatus.Authorized || e.Status == CLAuthorizationStatus.AuthorizedAlways || e.Status == CLAuthorizationStatus.AuthorizedWhenInUse)
                    {
                        LocationServicesStatus = LocationServicesStatusEnum.Ok;
                        this.tryCheckingNow();
                    }
                    else
                    {
                        Location = null; // location is not known
                        LocationServicesStatus = LocationServicesStatusEnum.NotAuthorized;
                        FailureInfo            = "Permission to use use location services not given";
                        fireEvents();
                    }
                };
                locationManager.RequestWhenInUseAuthorization();
                return;
            }

            // check location
            this.tryCheckingNow();
        }
Exemplo n.º 3
0
        void tryCheckingNow()
        {
            if (CLLocationManager.LocationServicesEnabled == false)
            {
                Location = null; // location is not known
                LocationServicesStatus = LocationServicesStatusEnum.NotAuthorized;
                FailureInfo            = "Not authorized to use location services (2)";
                fireEvents();
                return;
            }

            DateTime timeStarted = DateTime.Now;

            Task.Run(async() =>
            {
                for (; ;)
                {
                    await Task.Delay(100);

                    if (TimeUpdated != null && TimeUpdated.Value >= timeStarted)
                    {
                        // stop listening
                        // note: events had already been fired
                        locationManager.StopUpdatingLocation();
                        return;
                    }

                    if (TimeUpdated != null && (DateTime.Now - TimeUpdated.Value).TotalMinutes < 5 && Location != null)
                    {
                        // location already known. no need to wait.
                        fireEvents();
                        return;
                    }

                    if ((DateTime.Now - timeStarted).TotalSeconds > 5)
                    {
                        FailureInfo = "Timeout";
                        fireEvents();
                        return;
                    }
                }
            });

            // start listening
            locationManager.StartUpdatingLocation();
        }
Exemplo n.º 4
0
        //
        // ILocationListener
        //

        public void OnLocationChanged(Android.Locations.Location location)
        {
            this.TimeLocationUpdated = DateTime.Now;
            this.Location            = new Location()
            {
                Latitude = location.Latitude, Longitude = location.Longitude
            };
            this.LocationServicesStatus = LocationServicesStatusEnum.Ok;

            Console.WriteLine("OnLocationChanged: " +
                              location.Provider.ToString() + " " +
                              this.Location.Latitude.ToString() + " " +
                              this.Location.Longitude.ToString());

            this.fireEvents();

            try
            {
                locationManager.RemoveUpdates(this);
            }
            catch (Exception) {
            }
        }
Exemplo n.º 5
0
 public LocationService_Android()
 {
     LocationServicesStatus = LocationServicesStatusEnum.Unknown;
 }
Exemplo n.º 6
0
 public LocationService_iOS()
 {
     LocationServicesStatus = LocationServicesStatusEnum.Unknown;
 }