Google Maps

MyEclipse + JPA + Spring + Hibernate = DAO (mostly)

I just about have my Google Maps mashup working. When it’s deployed, it’ll show markers for each location where I’ve taught a course. Eventually I’ll filter them by year, connect them to home with polylines, and maybe more.

As a mashup, it’s not the most interesting application ever written, but it’s given me a good opportunity to learn the Google Maps API and to play with some other interesting technologies.

In this case, that means Spring 2.0 and the Java Persistence API (JPA). I needed some way to get my course data into and out of a database, and I certainly didn’t want to hardwire anything if I could avoid it. That meant I really needed a Data Access Object (DAO) layer for my courses.

I’ll describe the details over the next few posts, but here’s the good part. The newest version of MyEclipse, version 6.0, makes it extremely easy to add both Spring 2.0 libraries and JPA to a given project. The wizard provided asks for which JPA provider to use, with the available options being TopLink and Hibernate. Just because I’m familiar with it, I chose Hibernate.

(That lead to a serious bug later involving conflicts in the asm jar file, which I’ll also describe later.)

I also used Java 5 annotations to do the mappings, as in:

public class Location {
    @Id  @GeneratedValue
    private int id;

    private String city;
    private String state;
    private double latitude;
    private double longitude;

    // ... etc ...

I also used Spring’s @Transactional attribute in order to build a service layer that interacted with the CourseDAO. That was seriously easy, especially when I had the excellent Spring in Action book to use for reference. Since I already planned to use Spring’s dependency injection to manage the DAO classes into the service layer (i.e., I had Spring inject the transaction manager, the data source, the entity manager factory, and whatever annotation capabilities I needed), I let Spring manage as much as possible.

Just for completeness, I wrote my DAO tests in Groovy, too. 🙂

The process was all kinds of fun, actually. As soon as I decide the code is good enough, I’ll describe all the pieces here. In the meantime, I’m just rather insufferably pleased with myself.

Google Maps Groovy

Trivial Geocoding with Google and Groovy

I’m building a simple Google Maps mashup that will show where I’ve given classes over the past few years. It’s not much, but it’s an easy demonstration of the technology and, even better, an easy way for me to learn the Google Maps API and to play with Groovy some more.

As for Google Maps, the documentation online isn’t bad, but I have a better alternative. Scott Davis wrote an excellent introductory book for the technology called Google Maps API, v2. I bought the eBook at Pragmatic Programmers for a whopping $8.50. I’m glad to have the eBook, but it’s a bit of a shame, too, because I can’t think of a way to get it autographed when I see Scott at the No Fluff, Just Stuff conference in September.

One of the key elements of any Google map is the latitude and longitude of a particular location. To determine that, you need a geocoder, which is an application that turns place names into lat, long pairs. There are many free ones available on the Internet, but since I’m using Google anyway, I figured I might as well take advantage of theirs.

The data is available at a URL with the appropriate query parameters set. That’s an example of a RESTful web service, of course. Here’s the query for the home office of Kousen IT, Inc.:,+CT&output=csv&key=xyz…

I’m using the csv response type, which returns a string of the form:


The first element is the response code (200 for ok, 404 for not found, 610 for “you forgot to add in your key,” etc). The second element is the magnification level, and the last two elements are the latitude and longitude desired. There are also response types for XML, JSON, and others.

I’m going to put all my city, state locations in a database table, but just to test it out I wrote the following Groovy script:

def key = 'my Google Maps API key'
def cities = ['Marlborough','Camarillo','Boston','Winston-Salem']
def states = ['CT','CA','MA','NC']
for (i in 0..3) {
    def city = cities[i]
    def state = states[i]
    def query = "q=${city},+${state}&output=csv&key=${key}"
    def url_string = base + '?q=' +
        ',+' + URLEncoder.encode("${city}","UTF-8") +
        ',+' + state + "&output=csv&key=${key}"
    def results = new URL(url_string).text
    println "${city}, ${state}: " + 

I know there are better ways to store and access the data, but as I say I’m going to move to a database soon anyway. I used the URLEncoder class (from Java) to make sure that if I have any cities with spaces in the name (“Los Angeles”) it gets added to the URL in an encoded form (“Los+Angeles”).

The other parts I like are:

  1. Using the split(String) method in the String class to tokenize the String. I’m still used to using the old StringTokenizer class, but since Java 1.5 the split method has been available. I might as well get used to it.
  2. The beautiful array access from the end of the array [-2,-1] rather than from the beginning [2,3]. If they ever add any data to the response, I’m still fine as long as the lat and long are still the last two elements.
  3. Using the “spread-dot” operator to convert each element of the resulting string into a double.

If this code was going to be any longer I’d create a DAO class for the conversion. I might do that anyway, but this was so easy I couldn’t resist just writing it directly. The results were:

Marlborough, CT: [41.63257, -72.46314]
Camarillo, CA: [34.22291, -119.05074]
Boston, MA: [42.35864, -71.05665]
Winston-Salem, NC: [36.0996, -80.24105]

I still can’t get over how much easier it is to do anything in Groovy compared to raw Java. Programming is fun again. 🙂

Quick Google Maps test

Google maps claims you can add a map to any web page in three clicks. I thought I’d give it a try here. I’m planning to build a mashup featuring all the places I go to teach classes, but I’ll start small.

Here’s the code Google tells me to paste in here.

View Larger Map

I find it interesting that it’s embedded in an iframe tag. That feels so dated since Ajax became popular.