示例#1
0
        public async Task GetWeather(bool isUseGps = true)
        {
            var networkHelper = new NetworkHelper();

            if (_pos != null && isUseGps)
            {
                //pos available
                var b = new BasicGeoposition
                {
                    Latitude  = Pos.Coordinate.Point.Position.Latitude,
                    Longitude = Pos.Coordinate.Point.Position.Longitude
                };
                UpdateAddress();
                CurrentWeather = await _api.GetCityWeather(b);
            }
            else
            {
                BasicGeoposition b;

                //If in FixedLocation Mode then update address & GetWeather by FixedGeoLocation
                //else do normal
                if (!IsFixedLocation)
                {
                    if (GeoLocation != null && GeoLocation.Results.Any())
                    {
                        b = new BasicGeoposition
                        {
                            Latitude  = GeoLocation.Results[0].Geometry.Location.Lat,
                            Longitude = GeoLocation.Results[0].Geometry.Location.Lng
                        };
                        UpdateAddress();

                        CurrentWeather = await _api.GetCityWeather(b);
                    }
                    else
                    {
                        if (!networkHelper.HasInternetAccess)
                        {
                            return;
                        }

                        //Get user location when internet come back
                        await GetGeoLocation();

                        if (GeoLocation != null && GeoLocation.Results.Any())
                        {
                            b = new BasicGeoposition
                            {
                                Latitude  = GeoLocation.Results[0].Geometry.Location.Lat,
                                Longitude = GeoLocation.Results[0].Geometry.Location.Lng
                            };
                            UpdateAddress();

                            CurrentWeather = await _api.GetCityWeather(b);
                        }
                    }
                }
                else
                {
                    if (FixedGeoLocation != null && FixedGeoLocation.Results.Any())
                    {
                        b = new BasicGeoposition
                        {
                            Latitude  = FixedGeoLocation.Results[0].Geometry.Location.Lat,
                            Longitude = FixedGeoLocation.Results[0].Geometry.Location.Lng
                        };
                        CurrentWeatherInfo.Address = FixedGeoLocation.Results[0].FormattedAddress;

                        CurrentWeather = await _api.GetCityWeather(b);
                    }
                    else
                    {
                        if (!networkHelper.HasInternetAccess)
                        {
                            return;
                        }

                        //Get fixed location when internet come back
                        await GetGeoLocation();

                        if (FixedGeoLocation != null && FixedGeoLocation.Results.Any())
                        {
                            b = new BasicGeoposition
                            {
                                Latitude  = FixedGeoLocation.Results[0].Geometry.Location.Lat,
                                Longitude = FixedGeoLocation.Results[0].Geometry.Location.Lng
                            };
                            CurrentWeatherInfo.Address = FixedGeoLocation.Results[0].FormattedAddress;

                            CurrentWeather = await _api.GetCityWeather(b);
                        }
                    }
                }
            }
        }