GMapCivicAddressResolver / GMapCivicAddressResolver / AddressResolver.cs

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;
using System.Device.Location;
using System.Runtime.Serialization;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Collections.Generic;

namespace GMapCivicAddressResolver
{
    public class AddressResolver:ICivicAddressResolver
    {
        string language = "en-GB";
        public AddressResolver()
        {

        }
        public AddressResolver(string language)
        {
            this.language = language;
        }
        [DataContract]
        public class AddressInfo
        {
            [DataMember(Name = "formatted_address")]
            public string FormattedAddress { get; set; }
        }
        [DataContract]
        public class ResultInfo
        {
            [DataMember(Name = "results")]
            public AddressInfo[] Info { get; set; }
        }
        readonly string URITemplate = "http://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&sensor=true&language={2}";
        #region ICivicAddressResolver Members

        public CivicAddress ResolveAddress(GeoCoordinate coordinate)
        {
            throw new NotImplementedException("Use async version instead");
        }

        public void ResolveAddressAsync(GeoCoordinate coordinate)
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    var ainfo = ReadToObject<ResultInfo>(e.Result);
                    ResolveAddressCompleted(this, new ResolveAddressCompletedEventArgs(ToCivic(ainfo),e.Error,false,null));
                }
                else
                {
                    ResolveAddressCompleted(this, new ResolveAddressCompletedEventArgs(new CivicAddress(), e.Error, false, null));
                }
            };
            client.DownloadStringAsync(new Uri(GetFormattedUri(coordinate)));
        }

        private CivicAddress ToCivic(ResultInfo ainfo)
        {
            List<string> res = new List<string>();
            foreach (var single in ainfo.Info)
            {
                res.Add(single.FormattedAddress);
            }
            if (res.Count > 0)
                return new CivicAddress() { AddressLine1 = res[0] };
            else
                return new CivicAddress() { AddressLine1 = "#UNKNOWN#" };
        }

        public event EventHandler<ResolveAddressCompletedEventArgs> ResolveAddressCompleted = delegate { };

        #endregion
        private string GetFormattedUri(GeoCoordinate coord)
        {
            return string.Format(CultureInfo.InvariantCulture, URITemplate, coord.Latitude, coord.Longitude,language);
        }
        public static T ReadToObject<T>(string json) where T : class
        {
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            T res = ser.ReadObject(ms) as T;
            ms.Close();
            return res;
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.