Beispiel #1
0
        public List <WeatherData> GetWeatherForcast(Location location, WeatherUnits unit)
        {
            string url = BaseURL + "/data/2.5/forecast?";

            if (location is CityLocation)
            {
                CityLocation loc = (CityLocation)location;
                url += "q=" + loc.cityName;
                if (!System.String.IsNullOrEmpty(loc.countryCode))
                {
                    url += "," + loc.countryCode;
                }
            }
            else if (location is CoordinateLocation)
            {
                CoordinateLocation loc = (CoordinateLocation)location;
                url += "lat=" + loc.lat + "&lon=" + loc.lon;
            }
            else
            {
                throw new WeatherDataServiceException("unsupported location type");
            }
            if (unit == WeatherUnits.Celsius)
            {
                url += "&units=metric";
            }
            else if (unit == WeatherUnits.Fahrenheit)
            {
                url += "&units=imperial";
            }
            else if (unit == WeatherUnits.Kelvin)
            {
            }
            else
            {
                throw new WeatherDataServiceException("unsupported unit type");
            }
            url += "&appid=" + APIKey;

            DataContractJsonSerializer parser = new DataContractJsonSerializer(typeof(WeatherListJson));

            WeatherListJson    json = (WeatherListJson)parser.ReadObject((MakeWebRequest(url)));
            List <WeatherData> data = new List <WeatherData>();

            foreach (WeatherJson j in json.list)
            {
                data.Add(FromJsonToData(j, location, unit));
            }
            return(data);
        }
Beispiel #2
0
        public WeatherData GetWeatherData(Location location, WeatherUnits unit)
        {
            string url = BaseURL + "/data/2.5/weather?";

            if (location is CityLocation)
            {
                CityLocation loc = (CityLocation)location;
                url += "q=" + loc.cityName;
                if (!System.String.IsNullOrEmpty(loc.countryCode))
                {
                    url += "," + loc.countryCode;
                }
            }
            else if (location is Ziplocation)
            {
                Ziplocation loc = (Ziplocation)location;
                url += "zip=" + loc.zipCode + "," + loc.countryCode;
            }
            else if (location is CoordinateLocation)
            {
                CoordinateLocation loc = (CoordinateLocation)location;
                url += "lat=" + loc.lat + "&lon=" + loc.lon;
            }
            else
            {
                throw new WeatherDataServiceException("unsupported location type");
            }
            if (unit == WeatherUnits.Celsius)
            {
                url += "&units=metric";
            }
            else if (unit == WeatherUnits.Fahrenheit)
            {
                url += "&units=imperial";
            }
            else if (unit == WeatherUnits.Kelvin)
            {
            }
            else
            {
                throw new WeatherDataServiceException("unsupported unit type");
            }
            url += "&appid=" + APIKey;

            DataContractJsonSerializer parser = new DataContractJsonSerializer(typeof(WeatherJson));
            WeatherJson json = (WeatherJson)parser.ReadObject(MakeWebRequest(url));

            return(FromJsonToData(json, location, unit));
        }
Beispiel #3
0
        public float GetUVIndex(Location location)
        {
            if (location is CoordinateLocation)
            {
                CoordinateLocation loc = (CoordinateLocation)location;
                string             url = BaseURL + "/v3/uvi/" +
                                         loc.lat + "," + loc.lon + "/current.json?appid=" + APIKey;

                DataContractJsonSerializer parser = new DataContractJsonSerializer(typeof(UVJson));
                UVJson json = (UVJson)parser.ReadObject((MakeWebRequest(url)));
                return(json.data);
            }
            else
            {
                throw new WeatherDataServiceException("only CoordinateLocation is supported to get UV index");
            }
        }