当用户输入邮政编码时,我需要显示位置和城市名称。如何获取对应的位置名称?
6 回答
试试 USPS 邮政编码 API - http://www.usps.com/webtools/welcome.htm
您可以使用PlaceFinder地理编码 Web 服务,使用您想要解析为名称的邮政编码发出基于 REST 的请求。该服务支持 XML 和 JSON响应格式。这是服务返回的响应元素的列表。
使用 .NET,您将利用System.Net命名空间中的客户端或请求/响应类向服务发出请求并处理响应。
最简单的方法是使用字符串。如果您想花哨的话,您也可以创建一个 ZIP 类。
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");
}
}
}
有关 ZIP 的更多详细信息,请查看 Wikipedia
我在一家名为SmartyStreets的公司从事地址验证行业。这里介绍的解决方案都以多种方式发挥作用,但要注意它们的局限性和特殊性。例如,雅虎的服务更像是地址建议,而不是验证。USPS Web 服务返回的结果非常有限,例如:您不会获得地址的县和组件数据、实际可交付性等。
对于更灵活、免费的解决方案——我可以推荐我们的LiveAddress API吗?这是一个 REST-ful 端点,给定街道地址(例如)和邮政编码,将完整准确地完成整个地址。
或者,您可以使用https://thezipcodes.com/
This has almost all the data that I used for the search. Use http://thezipcodes.com/docs to use the API.