Saturday, October 24, 2009

Using JAK and Google Geocoder

In a previous post I mentioned that I would be doing some experimenting with the JAK - Java API for KML. I have now created a simple program that creates a KML file using co-ordinates obtained from calling the Google Geocoder.

If you wish to review the whole program I have made it available here (4mb).

The main parts of the code are as follows:

GeoCoder class
  • converts a street address into a longitude/latitude point.
  • call getLocation method with a string representing the address.
  • class uses Google Geocoder.

Refer to the Geocoder.java class file in the download for more information on how this works.



Main Class
  • assigns an address from the first command line argument
address = args[0];
  • calls the GeoCoder.getLocation method with Address String
location = Geocoder.getLocation(address).toString(); 
  • creates a KML file using the location value returned by the GeoCoder class
final Kml kml = new Kml();
kml.createAndSetPlacemark()
   .withName(address)
   .withOpen(Boolean.TRUE)
   .createAndSetPoint()
   .addToCoordinates(location);

try {
kml.marshal(new File("HelloKml.kml"));
} catch (FileNotFoundException ex) {
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

In the future I will expand on this application to provide more information in the KML.

1 comment:

  1. Hi.

    I trying to use a code similar to yours, but I get errors in:

    kml.createAndSetPlacemark()
    .withName(placemarkName).withOpen(Boolean.TRUE)
    .createAndSetPoint().addToCoordinates(-0.126236, 51.500152);

    The method createAndSetPlacemark is undefined for the type Kml.

    I'm using eclipse and I added libs:
    activation.jar, jaxb, etc..
    and JavaApiforKml.jar

    but no way.

    Could you please tell me what am I doing wrong.?

    Thanks for your post (I thought I was wrong with my code)

    ReplyDelete