We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be 开发者_如何学Goanswered with facts and citations.
Closed 4 years ago.
Improve this questionI am building an applet that requires an input of a location. however, because of the specific API that I'm using, that input must be Latitude and Longitude. the problem with this is that it would be inconvenient for users to figure out what their latitude and longitude is, and then type it in. however everybody knows their zip code. is there a Java API that can take a zip code, and return a latitude and longitude? (it can be from any random point inside the zip code, it accuracy doesn't really matter as long as that point is somewhere inside the zip code area) another thing that would work, is if it could get the location based on the ip address of the user. The last way that I was thinking of, was to look for ham radios in that zip code and get it's latitude and longitude. this was proposed to me by a friend that does alot of ham radio stuff, and he showed me this. is this possible to do?
Don't think there is an API, but the zip code database can be used for creating one.
This would be how you would do it in a regular application, it should be the same thing for applets.
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();
}
}
thanks to @Ring Bearer for the csv file link
Google (afaik) doesn't offer Zip Codes in their geocoding responses. As I'm currently working on something like that, I had to decide something pretty similar and found a work around: districts. The geocoding response holds the name of the country, state & district, so your users can narrow it down. Chained selections are a nice addition.
精彩评论