Created
February 26, 2018 17:22
-
-
Save DCCoder90/d9e11854f120325eebfba8600383560f to your computer and use it in GitHub Desktop.
A quick and dirty address validator using Google's Geocoding API. Could be easily expanded upon.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace Validation | |
{ | |
public class AddressComponent { | |
public string long_name { get; set; } | |
public string short_name { get; set; } | |
public List<string> types { get; set; } | |
} | |
public class Location { | |
public double lat { get; set; } | |
public double lng { get; set; } | |
} | |
public class Northeast { | |
public double lat { get; set; } | |
public double lng { get; set; } | |
} | |
public class Southwest { | |
public double lat { get; set; } | |
public double lng { get; set; } | |
} | |
public class Viewport { | |
public Northeast northeast { get; set; } | |
public Southwest southwest { get; set; } | |
} | |
public class Geometry { | |
public Location location { get; set; } | |
public string location_type { get; set; } | |
public Viewport viewport { get; set; } | |
} | |
public class Result { | |
public List<AddressComponent> address_components { get; set; } | |
public string formatted_address { get; set; } | |
public Geometry geometry { get; set; } | |
public string place_id { get; set; } | |
public List<string> types { get; set; } | |
} | |
public class AddressObject { | |
public List<Result> results { get; set; } | |
public string status { get; set; } | |
} | |
} |
This file contains hidden or 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
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using Validation.Properties; | |
namespace Validation | |
{ | |
public class AddressValidator{ | |
private readonly string _apiKey; | |
private readonly string _apiUrl = "https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}"; | |
public AddressValidator() { | |
_apiKey = Resources.google_geocoding; | |
} | |
public string GetFormattedAddress(string incompleteAddress) { | |
WebRequest request = WebRequest.Create(String.Format(_apiUrl, incompleteAddress, _apiKey)); | |
using (WebResponse response = request.GetResponse()) { | |
string text = new StreamReader(response.GetResponseStream()).ReadToEnd(); | |
AddressObject address = JsonConvert.DeserializeObject<AddressObject>(text); | |
return address.results[1].formatted_address; | |
} | |
} | |
public bool IsAddressValid(string address) { | |
WebRequest request = WebRequest.Create(String.Format(_apiUrl, incompleteAddress, _apiKey)); | |
using (WebResponse response = request.GetResponse()) { | |
string text = new StreamReader(response.GetResponseStream()).ReadToEnd(); | |
AddressObject validatedAddress = JsonConvert.DeserializeObject<AddressObject>(text); | |
return validatedAddress.status == "OK"; | |
} | |
} | |
public List<AddressComponent> GetAddressParts(string incompleteAddress) { | |
WebRequest request = WebRequest.Create(String.Format(_apiUrl, incompleteAddress, _apiKey)); | |
using (WebResponse response = request.GetResponse()) { | |
string text = new StreamReader(response.GetResponseStream()).ReadToEnd(); | |
AddressObject address = JsonConvert.DeserializeObject<AddressObject>(text); | |
return address.results[1].address_components; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment