I 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With