viernes, 25 de febrero de 2011

Obtención del WOEID de un lugar mediante Yahoo API usando C# en Windows Phone 7

Un WOEID es un identificador único de lugares en la tierra por sus siglas (Where On Earth IDentifier), fue introducido por Yahoo para identificar cualquier característica de la tierra. Es útil para identificar lugares (zonas) en las que caen las coordenadas de latitud y longitud, de un lugar.


El WOEID es útil para obtener información como por ejemplo el clima.



using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace F.Phone.Maps.Yahoo
{
    /// <summary>
    ///
    /// </summary>
    public class WOEIDArgs: EventArgs  {
        public string WOEID
        {
            get;
            set;
        }
    }

    /// <summary>
    ///
    /// </summary>
    public class GeoCode
    {
        public const string FindPlaceURL = "http://where.yahooapis.com/geocode?q={0},{1}&gflags=R&appid={2}";
        public const string WOEIDUrl = "http://where.yahooapis.com/v1/places.q('{0}')?appid={1}";
        public const string AppKey = "YOUR-APP-KEY--";
        WebClient proxy;
        WebClient Proxy {
            get {
                if (proxy == null)
                {
                    proxy = new WebClient();
                   
                }


                return proxy;
            }
        }
        public bool IsInError { get; set; }
        public string ErrorDescription { get; set; }
        public event EventHandler<WOEIDArgs> WOEIDFound;

        public void GetWOEID(double latitude, double longitude)
        {
            try
            {
               
                IsInError = false;
                Proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(WOEID_DownloadStringCompleted);
                Proxy.DownloadStringAsync(new Uri(
                        String.Format(FindPlaceURL , latitude.ToString ().Replace (',','.'), longitude.ToString ().Replace (',','.'), AppKey)
                    ));
            }
            catch (Exception ex)
            {
                IsInError = true;
                ErrorDescription = ex.Message;
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="placename"></param>
        public void GetWOEID(string placename) {
            try{
                IsInError = false ;
                Proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(WOEID_DownloadStringCompleted);
                Proxy.DownloadStringAsync(new Uri(
                        String.Format  (WOEIDUrl, placename, AppKey )
                    ));
            }
            catch (Exception ex){
                IsInError = true ;
                ErrorDescription = ex.Message ;
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WOEID_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (WOEIDFound != null)
                {
                    string text =
                    System.Text.RegularExpressions.Regex.Match(e.Result, @"\<woeid\>\s*(?<woeid>[0-9]+)\s*</woeid>").Groups["woeid"].ToString();

                    WOEIDArgs e1 = new WOEIDArgs();
                    e1.WOEID = text;
                 

                    WOEIDFound(sender, e1);
                }
            }
            catch (Exception ex)
            {
                IsInError = true;
                ErrorDescription = ex.Message;

            }
            finally {
                proxy = null;
            }
        }


    
    }
}


Transacciones Fiori

  /UI2/CACHE Register service for UI2 cache use /UI2/CACHE_DEL Delete cache entries /UI2/CHIP Chip Registration /UI2/CUST Customizing of UI ...