Created
January 30, 2014 23:22
-
-
Save btompkins/8722291 to your computer and use it in GitHub Desktop.
Geocode using Geocodio in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Geocodio | |
{ | |
public string ApiKey { get; set; } | |
public GeocodioResponse GeoCodeByCityState(string address) | |
{ | |
var client = new RestClient("http://api.geocod.io/v1/"); | |
// client.Authenticator = new HttpBasicAuthenticator(username, password); | |
var request = new RestRequest("geocode", Method.GET); | |
request.AddParameter("q", address); // adds to POST or URL querystring based on Method | |
request.AddParameter("api_key", ApiKey); | |
// execute the request | |
var response = client.Execute<GeocodioResponse>(request); | |
if (response.ErrorException == null) return response.Data; | |
const string message = "Error retrieving response. Check inner details for more info."; | |
var geocodioexception = new ApplicationException(message, response.ErrorException); | |
throw geocodioexception; | |
} | |
} | |
public class GeocodioResponse | |
{ | |
public List<GeocoioResults> Results { get; set; } | |
public class GeocoioResults | |
{ | |
public string FormattedAddress { get; set; } | |
public GeocodioLatLng Location { get; set; } | |
} | |
public class GeocodioLatLng | |
{ | |
public double Lat { get; set; } | |
public double Long { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment