我正在构建一个需要输入位置的applet。但是,由于我使用的特定API,该输入必须是纬度和经度。这样做的问题是,对于用户来说,找出他们的纬度和经度是什么,然后键入它是不方便的。然而,每个人都知道他们的邮政编码。有没有一种Java API可以接受邮政编码并返回纬度和经度?(它可以来自邮政编码内的任何随机点,只要该点在邮政编码区域内,它的准确性就无关紧要)另一件有效的事情是,它是否可以根据用户的ip地址获得位置。我想到的最后一种方法是在那个邮政编码中寻找业余无线电,并得到它的纬度和经度。这是一个做了很多业余无线电的朋友向我提出的,他给我看了this。这有可能做到吗?
发布于 2011-09-07 01:29:44
这将是你在常规应用程序中如何做的,对于applet也应该是一样的。
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文件链接
发布于 2011-09-01 23:04:09
不认为有应用程序接口,但是the zip code database可以用来创建一个。
发布于 2011-09-01 23:08:38
谷歌( Google ) doesn't offer Zip Codes在他们的地理编码响应中。由于我目前正在做这样的事情,我不得不决定做一些非常类似的事情,并找到了一个解决方案:地区。地理编码响应包含国家、州和地区的名称,因此您的用户可以缩小范围。链接选择是一个很好的补充。
https://stackoverflow.com/questions/7272259
复制相似问题