Map Adventures with Google, Oracle and Spring

1

In summer 2000 I started in the Field Service development team of Oracle’s eBusiness Suite (http://www.oracle.com/applications/service/srvonl_cont.html). The web form Dispatch Center contains four Java applets. One of them is a map. This map displays the location of the customers and the current position of the field service engineers who synchronize their mobile devices with a central database. The implementation of this map was quite problematic. Especially the performance caused severe headache. In 2005, five years and ten versions later, clicking the tab page of the map still was a receipt to freeze your application… then I saw Google Maps and I nearly fell from my chair. The performance is amazing. But also development work to integrate a map into an application is easy.

....

When you have geocoded data in your database, you can display it on a Google Map running in a browser without needing an applet or plugin. For example with Spring JDBC you can easily query that data and pass it on to a JSP view. Below is presented a code example that you can copy and modify in order to create your own web page that displays data on a map. It took me one rainy afternoon to find out how to use the Google Maps API and to create the most simple J2EE application that displays database data on Google Maps. This is indeed quicker than five years…

Just follow the six steps below and enjoy.

Step 1.    Download the project code

Get the code by downloading it here. Unzip it in a projects directory.

Step 2.    Build project with Maven

Make sure you have Maven 2 installed. If not, download it from http://maven.apache.org. Navigate to directory googlemap/ and – after verifying that pom.xml is there – launch command:

>mvn install

This will download the project dependencies from Open Source repository on the internet to a repository on your machine (hmm… still an old version of Spring, sorry). The Oracle JDBC drivers are not OS, so you need to add this to your local repository manually by first downloading e.g. Oracle 10g 10.2.0.1.0 JDBC drivers (ojdbc14.jar) from http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.html, and then issue the command:

>mvn install:install-file -Dfile=ojdbc14.jar-DgroupId=com.oracle.jdbc -DartifactId=ojdbc14 -Dversion=10.2.0.1.0-Dpackaging=jar -DgeneratePom=true

Now, we are ready to create an Eclipse project with the command:

>mvn eclipse:eclipse

Finally you should see a message that says that Eclipse project has been written to local directory googlemap/

Step 3.    Install Oracle database

Oracle Locator is a feature of Oracle Database 10g Standard and Enterprise Editions that provides core location functionality. Information can be found at http://www.oracle.com/technology/products/spatial/index.html. The free Express Edition can be downloaded at http://www.oracle.com/technology/software/products/database/xe/index.html.  Create user google/welcome. Grant sufficient privileges. Connect user google. Create objects and test data by running script googlemap/src/main/sql/create_tables_sdo.sql . Modify data source URL jdbc:oracle:thin:@localhost:1521:dev2 that is defined in file googlemap/src/main/webapp/META-INF/context.xml. Change the SID dev2 into you own SID.
If you have an older version of Oracle and you don’t have Locator installed as a separate option, you can still run this simple demo, by running the alternative script googlemap/src/main/sql/create_tables.sql. In this script the employee and customer data is geocoded in the most basic way by adding a Latitude and Longitude column to the tables. That works too in order to put data on a map (!), but of course you miss the rich geographic queries and related features – for instance asking the center of a collection of points and center the map accordingly, like is done in the example below.
If you want to add test data yourself, you’ll need to look for the latitudes and longitudes (don’t mix them up!). The Google documentation lists a number of free geocoders at http://www.google.com/search?q=free+geocoder.

Step 4.    Prepare Tomcat

Install Tomcat 5.5 (http://tomcat.apache.org) and add the following jar files to CATALINA_HOME/common/lib:
– commons-pool-1.2.jar
– commons-dbcp-1.2.1.jar
– commons-collections-3.1.jar
– ojdbc14.jar

Step 5.    Get your Google Maps API key

Google Maps is embedded into the web page by the Google Maps JavaScript API. In order to use the API you need to get an API key. You can sign up at http://www.google.com/apis/maps/signup.html. The key is linked to your Google account, so you can have only one URL. If you have a look at /WEB-INF/jsp/gmap.jsp, you’ll see an API key in the page head. This key is associated with the directory http://localhost:8081. You have to replace this key with your own key. Note that if your web server happens to listen to port 8081 too, you can use my key and you do not need to modify the file gmap.jsp.

Step 6.    Launch the demo

Add directory .deployables under googlemap/. Import project into Eclipse (or other IDE). Run index.html on server. The map should appear right away. Congratulations!

The Google Maps API documentation is at http://www.google.com/apis/maps/documentation/#The_Basics. A tutorial is offered at http://www.econym.demon.co.uk/googlemaps.

Share.

About Author

1 Comment

  1. A wonderful job, a great introduction to Google Maps. My fingers are itching to get started right away. I am sure the customer you built the application for that has this functionality embedded will be most impressed. Thanks for this article.

    Lucas