我正在开发一个小程序,需要输入位置信息。但是由于我使用的特定API,输入必须是纬度和经度。问题在于,用户找出自己的纬度和经度并键入会很不方便,但每个人都知道自己的邮政编码。是否有Java API可以根据邮政编码返回纬度和经度(可以是邮政编码内的任意随机点,精度并不重要,只要该点位于邮政编码区域内)。另一种方法是根据用户的IP地址获取位置信息。我还考虑过另一种方式,即查找该邮政编码中的业余无线电台,并获取其纬度和经度。我的朋友曾向我介绍过一个网站(点击此处),他认为这样做是可行的。
我认为没有相关的API,但可以使用邮政编码数据库来创建一个。
public static void main(String[] args) {
/** let's use the following zip code simply for the purpose of this
example (its the zip code for hollywood callifornia) */
String zipCode = 91601
String latitude = "0";
String longitude = "0";
try {
/**
JavaCVS api is required in order to read this, it can
be found at http://sourceforge.net/projects/javacsv/
**/
CsvReader products = new CsvReader("zips.csv");
/** a cvs containing all the zip codes and latitudes
and longitudes can be found at:
http://sourceforge.net/projects/zips/files/zips/zips.csv.zip/
**/
products.readHeaders();
int numOfHeaders = products.getHeaderCount();
System.out.println("Number of headers" + numOfHeaders);
try {
while (products.readRecord())
{
String lookedupZip = products.get(products.getHeader(0));
if (lookedupZip.equals(zipCode)) {
latitude = products.get(products.getHeader(2));
longitude = products.get(products.getHeader(3));
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
感谢@Ring Bearer提供的CSV文件链接。
据我所知,Google在他们的地理编码响应中不提供邮政编码。 因为我目前正在处理类似的事情,所以我不得不决定一些相似的东西并找到了一个解决方法:区域。 地理编码响应包含国家,州和区域的名称,因此您的用户可以缩小范围。 链式选择是很好的添加。