CodePaste Logo
New Snippet New Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: csharp

Yahoo GeocodeHelper for WinRT

762 Views   
/// <summary>Search Yahoo Local</summary> /// <see cref="http://developer.yahoo.com/geo/placefinder/"/> public class GeocodeHelper { private string YahooKey; public GeocodeHelper(string yahooKey) { this.YahooKey = yahooKey; } public async Task<ResultSet> Search(string query) { var _Url = "http://where.yahooapis.com/geocode?flags=j&"; _Url += string.Format("appid={0}&", System.Uri.EscapeDataString(YahooKey.ToString())); _Url += string.Format("q={0}&", System.Uri.EscapeDataString(query)); _Url = _Url.TrimEnd('&'); string _JsonString = string.Empty; try { // fetch from rest service var _HttpClient = new System.Net.Http.HttpClient(); var _HttpResponse = await _HttpClient.GetAsync(_Url); _JsonString = await _HttpResponse.Content.ReadAsStringAsync(); // check for error if (_JsonString.Contains("{\"Error\":")) throw new InvalidResultException { JSON = _JsonString }; // deserialize json to objects var _JsonBytes = Encoding.Unicode.GetBytes(_JsonString); using (MemoryStream _MemoryStream = new MemoryStream(_JsonBytes)) { var _JsonSerializer = new DataContractJsonSerializer(typeof(RootObject)); var _Result = (RootObject)_JsonSerializer.ReadObject(_MemoryStream); return _Result.ResultSet; } } catch (InvalidSomethingException) { throw; } catch (Exception e) { throw new InvalidSomethingException(e) { JSON = _JsonString }; } } public class InvalidSomethingException : Exception { public InvalidSomethingException() { } public InvalidSomethingException(Exception e) : base(string.Empty, e) { } public string JSON { get; set; } } public class InvalidCredentialsException : InvalidSomethingException { } public class InvalidResultException : InvalidSomethingException { } public class Result { public int quality { get; set; } public string latitude { get; set; } public string longitude { get; set; } public string offsetlat { get; set; } public string offsetlon { get; set; } public int radius { get; set; } public string name { get; set; } public string line1 { get; set; } public string line2 { get; set; } public string line3 { get; set; } public string line4 { get; set; } public string house { get; set; } public string street { get; set; } public string xstreet { get; set; } public string unittype { get; set; } public string unit { get; set; } public string postal { get; set; } public string neighborhood { get; set; } public string city { get; set; } public string county { get; set; } public string state { get; set; } public string country { get; set; } public string countrycode { get; set; } public string statecode { get; set; } public string countycode { get; set; } public string uzip { get; set; } public string hash { get; set; } public int woeid { get; set; } public int woetype { get; set; } // helpers public double Latitude { get { return double.Parse(this.latitude); } } public double Longitude { get { return double.Parse(this.longitude); } } } public class ResultSet { public string version { get; set; } public int Error { get; set; } public string ErrorMessage { get; set; } public string Locale { get; set; } public int Quality { get; set; } public int Found { get; set; } public List<Result> Results { get; set; } } public class RootObject { public ResultSet ResultSet { get; set; } } }
by Jerry Nixon
  June 14, 2012 @ 1:56pm
Tags:
Add a comment


Report Abuse
brought to you by:
West Wind Techologies