Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/MapMainDemo/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/MapMainDemo/Modules/WorldWeather.cs

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using DevExpress.Utils;
using System.Xml.Linq;

namespace DevExpress.XtraMap.Demos {

    public partial class WorldWeather : MapTutorialControl {
        OpenWeatherMapService openWeatherMapService;
        CityWeather actualWeatherInfo;
        TemperatureMeasureUnits actualMeasureUnits = TemperatureMeasureUnits.Celsius;

        protected OpenWeatherMapService OpenWeatherMapService { get { return openWeatherMapService; } }
        ImageTilesLayer TilesLayer { get { return (ImageTilesLayer)(mapControl1.Layers[0]); } }
        VectorItemsLayer ItemsLayer { get { return (VectorItemsLayer)(mapControl1.Layers[1]); } }

        public WorldWeather() {
            InitializeComponent();

            TilesLayer.DataProvider = CreateBingDataProvider(BingMapKind.Area);
            mapControl1.SetMapItemFactory(new DemoWeatherItemFactory());

            this.openWeatherMapService = new OpenWeatherMapService(LoadCapitalsFromXML());
            OpenWeatherMapService.ReadCompleted += OpenWeatherMapService_ReadCompleted;
            openWeatherMapService.GetWeatherAsync();
        }
        List<string> LoadCapitalsFromXML() {
            List<string> capitals = new List<string>();
            XDocument document = DemoUtils.LoadXml("Capitals.xml");
            if (document != null) {
                foreach (XElement element in document.Element("Capitals").Elements()) {
                    capitals.Add(element.Value);
                }
            }
            return capitals;
        }
        void OpenWeatherMapService_ReadCompleted(object sender, EventArgs e) {
            ItemsLayer.DataSource = OpenWeatherMapService.WeatherInCities;
            ItemsLayer.SelectedItem = OpenWeatherMapService.LosAngelesWeather;
            OpenWeatherMapService.ReadCompleted -= OpenWeatherMapService_ReadCompleted;
        }

        void rgMode_SelectedIndexChanged(object sender, EventArgs e) {
            if (actualWeatherInfo != null) {
                if (rgMode.SelectedIndex == 0) {
                    actualMeasureUnits = TemperatureMeasureUnits.Fahrenheit;
                    this.chartControl1.Series[0].ValueDataMembers[0] = "Weather.FahrenheitTemperature";
                }
                if (rgMode.SelectedIndex == 1) {
                    actualMeasureUnits = TemperatureMeasureUnits.Celsius;
                    this.chartControl1.Series[0].ValueDataMembers[0] = "Weather.CelsiusTemperature";
                }
                lbTemperature.Text = actualWeatherInfo.Weather.GetTemperatureString(actualMeasureUnits);
                ItemsLayer.Mappings.Text = actualWeatherInfo.GetTemperatureDataMember(actualMeasureUnits);
            }
        }

        void mapControl1_SelectionChanged(object sender, MapSelectionChangedEventArgs e) {
            IList<object> selection = e.Selection;
            if (selection == null || selection.Count != 1)
                return;
            CityWeather cityWeatherInfo = selection[0] as CityWeather;
            MapItem mapItem = selection[0] as MapItem;
            if (mapItem != null)
                cityWeatherInfo = ItemsLayer.GetItemSourceObject(mapItem) as CityWeather;
            this.actualWeatherInfo = cityWeatherInfo;
            if(cityWeatherInfo != null) {
                if(cityWeatherInfo.Forecast == null) {
                    OpenWeatherMapService.GetForecastForCityAsync(cityWeatherInfo);
                    cityWeatherInfo.ForecastUpdated += cityWeatherInfo_ForecastUpdated;
                }
                else
                    cityWeatherInfo_ForecastUpdated(cityWeatherInfo, null);
            }
        }
        void cityWeatherInfo_ForecastUpdated(object sender, EventArgs e) {
            CityWeather cityWeatherInfo = sender as CityWeather;
            Action<CityWeather> del = LoadWeatherPicture;
            BeginInvoke(del, cityWeatherInfo);
        }
        void LoadWeatherPicture(CityWeather cityWeatherInfo) {
            this.chartControl1.Series[0].DataSource = cityWeatherInfo.Forecast;
            lbCity.Text = cityWeatherInfo.City;
            lbTemperature.Text = cityWeatherInfo.Weather.GetTemperatureString(actualMeasureUnits);
            peWeatherIcon.LoadAsync(cityWeatherInfo.WeatherIconPath);
        }
        private void mapControl1_SelectionChanging(object sender, MapSelectionChangingEventArgs e) {
            e.Cancel = e.Selection.Count == 0;
        }
    }
}