Exemple #1
0
        private bool TryGetElevation(GpsLocation location, out double altitude)
        {
            var et = new ElevationTile(location);

            if (et.Exists())
            {
                if (et.LoadFromZip())
                {
                    altitude = et.GetElevation(location);
                    return(true);
                }
            }

            altitude = 0;
            return(false);
        }
Exemple #2
0
        public async System.Threading.Tasks.Task <GpsLocation> GetLocationAsync()
        {
            try
            {
                if (AppContextLiveData.Instance.Settings.IsManualLocation)
                {
                    return(AppContextLiveData.Instance.Settings.ManualLocation);
                }

                if (waitingForResponse)
                {
                    return(null);
                }

                var request = new GeolocationRequest(GeolocationAccuracy.Best, new TimeSpan(0, 0, 0, 5));

                waitingForResponse = true;
                Location location = await Geolocation.GetLocationAsync(request);

                if (location != null)
                {
                    currentLocation.Latitude  = location.Latitude;
                    currentLocation.Longitude = location.Longitude;
                    currentLocation.Altitude  = location.Altitude.Value;

                    if (AppContextLiveData.Instance.Settings.AltitudeFromElevationMap)
                    {
                        if (_elevationTile == null || !_elevationTile.HasElevation(currentLocation))
                        {
                            _elevationTile = null;
                            var et = new ElevationTile(currentLocation);
                            if (et.Exists())
                            {
                                if (et.LoadFromZip())
                                {
                                    _elevationTile = et;
                                }
                            }
                        }

                        if (_elevationTile != null)
                        {
                            currentLocation.Altitude = _elevationTile.GetElevation(currentLocation);
                        }
                    }

                    return(currentLocation);
                }

                return(null);
            }
            catch (FeatureNotSupportedException ex)
            {
                throw new Exception($"GPS is not supported. {ex.Message}");
            }
            catch (FeatureNotEnabledException ex)
            {
                throw new Exception($"GPS is not enabled. {ex.Message}");
            }
            catch (PermissionException ex)
            {
                throw new Exception($"GPS is not allowed. {ex.Message}");
            }
            catch (Exception ex)
            {
                throw new Exception($"Error when fetching GPS location. {ex.Message}");
            }
            finally
            {
                waitingForResponse = false;
            }
        }