[Solved] Console application that validates address [closed]


One way to do it would be to use Googles geocode API. You can pass it a partial address and it will do it’s best to return the normalized address for you. If the address isn’t very specific, you will get back more than one.

Here’s a code example for calling the API and parsing the results:

private static List<string> GetNormalizedAddresses(string address)
{
    // Generate request Uri
    var baseUrl = "http://maps.googleapis.com/maps/api/geocode/xml";
    var requestUri = $"{baseUrl}?address={Uri.EscapeDataString(address)}&sensor=false";

    // Get response
    var request = WebRequest.Create(requestUri);
    var response = request.GetResponse();
    var xDoc = XDocument.Load(response.GetResponseStream());
    var results = xDoc.Element("GeocodeResponse")?.Elements("result").ToList();

    var normalizedAddresses = new List<string>();

    // Populate results
    if (results != null)
    {
        normalizedAddresses.AddRange(results
            .Select(result => result.Element("formatted_address")?.Value)
            .Where(formattedAddress => !string.IsNullOrWhiteSpace(formattedAddress)));
    }

    return normalizedAddresses;
}

Then, you could call it like so:

while(true)
{
    Console.Write("Enter a partial address: ");
    var partialAddress = Console.ReadLine();
    Console.WriteLine(new string ('-', 25 + partialAddress.Length));

    var normalizedAddress = GetNormalizedAddresses(partialAddress);

    if (!normalizedAddress.Any())
    {
        Console.WriteLine("Sorry, couldn't find anything.");
    }
    else
    {
        Console.WriteLine("That address normalizes to:");
        Console.WriteLine($" - {string.Join($"\n - ", normalizedAddress)}");
    }

    Console.WriteLine("\n");        
}

Output

enter image description here

1

solved Console application that validates address [closed]