Ejemplo n.º 1
0
        // Simple GPS tracking =)
        private async void GetCoordinates()
        {
            Debug.WriteLine("PLAN A - Try to get coordinates");
            // PLAN A ("iOS / Android")
            try
            {
                // uh, its no working for UWP mode :(
                var request  = new GeolocationRequest(GeolocationAccuracy.Best);
                var location = await Geolocation.GetLocationAsync(request);

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

                    // uh, its no working for UWP mode , too !
                    Location = await GetCity(location);

                    GetWeatherInfo();

                    return;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Ex. msg:" + ex.Message);
            }

            // PLAN B ("UWP")

            Debug.WriteLine("PLAN B - geo Info.GetStatus");

            // Dirty Hack: redo it!!!
            //while (geoInfo.GetLatitude() == 0) { };

            // if status "Access to location is denied"...
            if (geoInfo.GetStatus() == 2)
            {
                //TODO
                //await DisplayAlert("Caution", "Access to location is denied. Please unblock it (change Weather app permission)", "OK");
                Debug.WriteLine("Caution: access to location is denied! Please unblock it (change Weather app permission)");
            }

            Latitude  = geoInfo.GetLatitude();
            Longitude = geoInfo.GetLongitude();

            // Experimental!
            Location = await GetCityName(Latitude, Longitude);

            Debug.WriteLine("[debug] GetCity: " + Location);

            GetWeatherInfo();

            return;
        }
Ejemplo n.º 2
0
        public MainPage()
        {
            int counter = 0;

            InitializeComponent();

            // !!! Interface Init
            geoInfo = DependencyService.Get <IGeoInfo>();

            // Try to start geolocation tracking...
            geoInfo.StartTracking();

            int status = geoInfo.GetStatus();

            while ((status != 2) && (status != 5))
            {
                status = geoInfo.GetStatus();
                // wait gps's init
                //Debug.WriteLine("Wait gps's init... geoInfo.GetStatus=" + status);

                counter++;
                if (counter == 3000)
                {
                    //await
                    DisplayAlert("Alert", "GPS's init failed", "OK");

                    GetCoordinates();

                    return;
                }
            }

            // Stop geolocation tracking

            geoInfo.StopTracking();


            //GetWeatherInfo();
            GetCoordinates();
        }