Get location name by giving ZIP codes

时光怂恿深爱的人放手 提交于 2019-12-06 14:07:05

I would use a website like

http://www.zipinfo.com/search/zipcode.htm

and just send the zipcode to that, retrieve the input, parse for the city name, easy as that.

Try the USPS zipcode API - http://www.usps.com/webtools/welcome.htm

You can use the PlaceFinder geocoding web service to make REST based requests using the postal code you want to resolve to a name. The service supports both XML and JSON response formats. Here is a listing of the response elements returned by the service.

Using .NET, you would leverage the client or request/response classes in the System.Net namespace to make a request to the service and process the reponse.

The simplest way would be to use strings. You could alternatively create a ZIP class, if you wanted to get fancy.

using System;
using System.Collections.Generic;

class Program
{
    // declare your variable
    private static Dictionary<string, string> zipLookup;

    public static void CreateZips()
    {
        zipLookup = new Dictionary<string, string>();
        zipLookup.Add("90210", "Beverly Hills");
        // fill all other values, probably from a db
    }

    static void Main(string[] args)
    {
        CreateZips();

        var test  = "90210";

        if (zipLookup.ContainsKey(test))
        {
            Console.WriteLine(test.ToString() + "=" + zipLookup[test]);
        }
        else
        {
            Console.WriteLine(test.ToString() + " location unknown");
        }
    }
}

For more details on ZIPs, check out Wikipedia

I work in the address verification industry for a company called SmartyStreets. The solutions presented here are all functional in a variety of ways, but beware of their limitations and specialties. For example, Yahoo's service is more like address suggestion, not validation. The USPS web service is quite limited in the results it returns, for example: you won't get the County and Component data of an address, actual deliverability, etc.

For a more flexible, free solution -- may I suggest our LiveAddress API? It's a REST-ful endpoint which, given a street address (for example) and ZIP code, will fully and accurately complete the entire address.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!