Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a java api for getting the latitude and longitude from zip codes? [closed]

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?

like image 375
Chan.... Avatar asked Feb 23 '23 02:02

Chan....


2 Answers

Don't think there is an API, but the zip code database can be used for creating one.

like image 108
ring bearer Avatar answered Apr 25 '23 09:04

ring bearer


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

like image 27
Ephraim Avatar answered Apr 25 '23 08:04

Ephraim