Skip to content

Instantly share code, notes, and snippets.

@DCCoder90
Created February 26, 2018 17:22
Show Gist options
  • Save DCCoder90/d9e11854f120325eebfba8600383560f to your computer and use it in GitHub Desktop.
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.
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; }
}
}
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