Showing posts with label kml. Show all posts
Showing posts with label kml. Show all posts

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.

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.

Friday, October 9, 2009

JAK - Java API for KML

In a previous blog post I showed how it was possible to take information recorded by a GPS device and mobile phone and plot the resulting track using Google Maps.

Recently, I found a link to a JAVA API that allows you to interact with the KML files produced by the process using a JAVA application.

The project aims to provide an

"automatically generated full reference implementation of the KML object model defined by OGC’s KML standard and Google’s GX extensions. It is an object orientated API that enables the convenient and easy use of KML in existing Java environments."
Over the next couple of weeks I am going to do some experimentation using this API and try and develop some simple applications to take GPS tracks from Nokia Sports Tracker and my GPS and create interesting KML files.

Stay tuned...



Sunday, June 21, 2009

GPS Mapping

Over the last 10 years I have ridden many of the trails in the Central Coast area, often more than once. Recently, I have started to map these trails using a GPS with the aim to document them so other riders may enjoy them.

To do the mapping I use the following hardware and software.

Hardware

Software


An example of the embedded map produced by this technique is below. It shows the various information available from the file produced from Nokia Sports Tracker.


View Larger Map

Capturing the Data Points (GPS Track)

To capture the data used for the KML file I use my Nokia mobile phone connected to a bluetooth GPS Module. Nokia have released an application called Nokia Sports Tracker which allows you to record a GPS track. For further information on the use of Nokia Sports Tracker visit the Nokia website.

The Nokia Sports Tracker application has the option to output the file of data points to a variety of formats (GPS, CSV, or KML). We are most interested in KML here because it allows us to import the track into Google Earth or Google Maps.

Viewing the Map

Now that we have a KML file we can open this file directly into Google Earth to check that it looks correct. If you have Google Earth installed, double-clicking the KML file in Windows will open the path and display.

To use the path in Google Maps is a little more involved as you need to host the KML file somewhere on the web. I use a folder on my hosting provider.

Once you have the KML file hosted on the web you can now use it within Google Maps. To use within Google Maps copy/paste the URL into the Google Maps Search Bar and press 'Search' button.


Google Maps will now display the path on the map with some additional information about the path from the KML file.


Embedding the Map

Once you have the map displayed you can now Print, Send, or Link to this map. To embed this map into a blog or another web page use the 'Link' menu and paste the HTML in the second box into your website code.

You can use the 'Customise and preview embedded map' to change the settings of the embedded map.

For more information refer to the examples on the Google Maps site.