Saturday, November 7, 2009

Using JAK and Google Geocoder Part 2

In this second post about using JAK and Google Geocoder I have included a new application that processes a CSV file containing a list of descriptions and addresses. It uses a library from http://ostermiller.org/utils/ which simplifies the reading of CSV files.

The following code snippet shows the usage of the CSVParser class from OsterMiller.org.

String addresses[][] = null;
try {
   CSVParser csv = new CSVParser(new FileReader("restaurants.csv"));
     try {
         addresses = csv.getAllValues();
         } catch (IOException ex) {
         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
         }
   } catch (FileNotFoundException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      }

Once the string array has been loaded with values we can iterate through the array and use the Geocoder class to find the latitude and longitude of each of the addresses in the file. The code below shows this process and the adding each of the placemarks into the KML file.

for (int i=0; i<addresses.length; i++){
  try {            
      location1 = Geocoder.getLocation(addresses[i][1]).toString();
      } catch (IOException ex) {
         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
      }
      final Placemark placemark1 = document.createAndAddPlacemark()
                  .withName(addresses[i][0])
                  .withDescription(addresses[i][1] + addresses[i][2]);
      placemark1.createAndSetPoint().addToCoordinates(location1);
      }

The full code for this project is here. I hope that you find it useful.

No comments:

Post a Comment