Просмотр исходного кода

Merge commit 'c9e3b9f74f13d5ced26ff2f87fe5c0db8fe9794f' into nick

Nick-PRSDigital@bitbucket.org 1 год назад
Родитель
Сommit
efd21ee732
1 измененных файлов с 85 добавлено и 74 удалено
  1. 85 74
      InABox.Mobile/InABox.Mobile.Shared/Geolocation.cs

+ 85 - 74
InABox.Mobile/InABox.Mobile.Shared/Geolocation.cs

@@ -79,115 +79,126 @@ namespace InABox.Mobile
             // Don't reset this on every refresh, otherwise the final UI will randomly get "Searching for GPS" as the address
             //Address = "Searching for GPS";
 
+            bool bOK = MobileUtils.IsPermitted<Permissions.LocationWhenInUse>().Result;
+            
             Task.Run(async () =>
             {
 
-                bool bOK = await MobileUtils.IsPermitted<Permissions.LocationWhenInUse>();
-
-                if (!bOK)
-                {
-                    Latitude = 0.0F;
-                    Longitude = 0.0F;
-                    TimeStamp = DateTime.MinValue;
-                    Address = "GPS Services Disabled";
-                    Device.BeginInvokeOnMainThread(() =>
-                    {
-                        OnLocationError?.Invoke(this, new Exception("Please enable GPS Services to continue"));
-                    });
-                    bOK = false;
-                    bLocating = false;
-                }
-                else
+                try
                 {
 
-                    try
+
+                    
+
+                    if (!bOK)
+                    {
+                        Latitude = 0.0F;
+                        Longitude = 0.0F;
+                        TimeStamp = DateTime.MinValue;
+                        Address = "GPS Services Disabled";
+                        Device.BeginInvokeOnMainThread(() =>
+                        {
+                            OnLocationError?.Invoke(this, new Exception("Please enable GPS Services to continue"));
+                        });
+                        bOK = false;
+                        bLocating = false;
+                    }
+                    else
                     {
-                        var request = new GeolocationRequest(GeolocationAccuracy.Best, new TimeSpan(0, 0, 20));
-                        var location = await Geolocation.GetLocationAsync(request);
 
-                        if (location != null)
+                        try
                         {
+                            var request = new GeolocationRequest(GeolocationAccuracy.Best, new TimeSpan(0, 0, 20));
+                            var location = await Geolocation.GetLocationAsync(request);
 
-                            //if (location.IsFromMockProvider)
-                            //{
-                            //    Device.BeginInvokeOnMainThread(() =>
-                            //    {
-                            //        OnLocationError?.Invoke(this, new Exception("Mock GPS Location Detected!\nPlease correct and restart TimeBench."));
-                            //    });
-                            //}
-                            //else
+                            if (location != null)
                             {
 
-                                Latitude = location.Latitude;
-                                Longitude = location.Longitude;
-                                TimeStamp = DateTime.Now;
+                                //if (location.IsFromMockProvider)
+                                //{
+                                //    Device.BeginInvokeOnMainThread(() =>
+                                //    {
+                                //        OnLocationError?.Invoke(this, new Exception("Mock GPS Location Detected!\nPlease correct and restart TimeBench."));
+                                //    });
+                                //}
+                                //else
+                                {
 
+                                    Latitude = location.Latitude;
+                                    Longitude = location.Longitude;
+                                    TimeStamp = DateTime.Now;
 
-                                String sErr = "";
-                                Placemark address = null;
-                                try
-                                {
-                                    var addresses = await Geocoding.GetPlacemarksAsync(Latitude, Longitude);
-                                    double maxdist = double.MaxValue;
-                                    foreach (var cur in addresses.Where(x => !String.IsNullOrEmpty(x.Thoroughfare)))
+
+                                    String sErr = "";
+                                    Placemark address = null;
+                                    try
                                     {
-                                        var delta = Location.CalculateDistance(location, cur.Location, DistanceUnits.Kilometers);
-                                        if (delta < maxdist)
+                                        var addresses = await Geocoding.GetPlacemarksAsync(Latitude, Longitude);
+                                        double maxdist = double.MaxValue;
+                                        foreach (var cur in addresses.Where(x => !String.IsNullOrEmpty(x.Thoroughfare)))
                                         {
-                                            address = cur;
-                                            maxdist = delta;
+                                            var delta = Location.CalculateDistance(location, cur.Location, DistanceUnits.Kilometers);
+                                            if (delta < maxdist)
+                                            {
+                                                address = cur;
+                                                maxdist = delta;
+                                            }
                                         }
+
+                                    }
+                            
+                                    catch (Exception ee2)
+                                    {
+                                        sErr = ee2.Message;
+                                        //address = null;
                                     }
+                                    if (address != null)
+                                        Address = String.Format("{0} {1} {2}", address.SubThoroughfare, address.Thoroughfare, address.Locality);
+                                    else
+                                        Address = String.Format("Lat: {0}, Lng: {1}", Latitude, Longitude);
 
-                                }
-                        
-                                catch (Exception ee2)
-                                {
-                                    sErr = ee2.Message;
-                                    //address = null;
-                                }
-                                if (address != null)
-                                    Address = String.Format("{0} {1} {2}", address.SubThoroughfare, address.Thoroughfare, address.Locality);
-                                else
-                                    Address = String.Format("Lat: {0}, Lng: {1}", Latitude, Longitude);
+                                    if (location.IsFromMockProvider)
+                                        Address = "** " + Address;
 
-                                if (location.IsFromMockProvider)
-                                    Address = "** " + Address;
+                                    if (!String.IsNullOrEmpty(sErr))
+                                        Address = String.Format("{0} (ERROR: {1})", Address, sErr);
 
-                                if (!String.IsNullOrEmpty(sErr))
-                                    Address = String.Format("{0} (ERROR: {1})", Address, sErr);
+                                    Device.BeginInvokeOnMainThread(() =>
+                                    {
+                                        OnLocationFound?.Invoke(this);
+                                    });
+                                    bLocating = false;
+                                }
 
+                            }
+                            else
+                            {
+                                Latitude = 0.00;
+                                Longitude = 0.00;
+                                TimeStamp = DateTime.MinValue;
+                                bLocating = false;
                                 Device.BeginInvokeOnMainThread(() =>
                                 {
-                                    OnLocationFound?.Invoke(this);
+                                    OnLocationError?.Invoke(this, new Exception("Unable to get GPS Location"));
                                 });
-                                bLocating = false;
+
                             }
 
                         }
-                        else
+                        catch (Exception e)
                         {
-                            Latitude = 0.00;
-                            Longitude = 0.00;
-                            TimeStamp = DateTime.MinValue;
                             bLocating = false;
                             Device.BeginInvokeOnMainThread(() =>
                             {
-                                OnLocationError?.Invoke(this, new Exception("Unable to get GPS Location"));
+                                OnLocationError?.Invoke(this, e);
                             });
-
                         }
-
-                    }
-                    catch (Exception e)
-                    {
-                        bLocating = false;
-                        Device.BeginInvokeOnMainThread(() =>
-                        {
-                            OnLocationError?.Invoke(this, e);
-                        });
                     }
                 }
+                catch (Exception e)
+                {
+                    
+                }
 
                 bLocating = false;
             });