Archive for June 16th, 2008

Map Application in Swing - The Easy Way

Ever tried to visually represent geographical information in your Swing Application? Wouldn't it be nice to have something similar to Google maps available as a Swing component. Many thanks to the guys at SwingLabs who provide such an easy way to integrate mapping into your own Swing application. I'll try to illustrate the very basic functionality of the mapping component JXMapKit from the SwingX-WS project.
First of I'll give you a hint how the final application will look like:
Main Window of the Euro2008 - Host Cities Viewer
The application shows the location of the host cities of the Euro 2008 in a map.

Now to the interesting part - the Java code. First of all we have to instantiate an object from type JXMapKit:

JAVA:
  1. JXMapKit mapViewer = new JXMapKit();
  2. mapViewer.setDefaultProvider(
  3.   DefaultProviders.OpenStreetMaps);
  4. mapViewer.setDataProviderCreditShown(true);

Add the mapViewer to a JFrame and that's all you need if you would like to show the user a map from OpenStreetMap centered at London with controls for zooming and a minimap. If you would like to change where the map is centered you simply have to instantiate a GeoPosition object with coordinates and call the setAddressLocation on the JXMapKit object. For example if you would like to center the map at the city of the Euro 2008 final

JAVA:
  1. GeoPosition vienna =
  2.   new GeoPosition(48.20875, 16.372583);
  3. mapViewer.setAddressLocation(vienna);

Adding Waypoints
Waypoints are sets of coordinates that identify a point in physical space (Quote from Wikipedia). To complete the "Euro2008 - Host Cities Viewer" Application we finally add a marker on the map at the position of the eight host cities.

JAVA:
  1. GeoPosition zuerich =
  2.   new GeoPosition(47.366667, 8.55);
  3. GeoPosition basel =
  4.   new GeoPosition(47.566667, 7.6);
  5. ...
  6.    
  7. Set<Waypoint> hostCitiesEuro08 =
  8.   new HashSet<Waypoint>();
  9. hostCitiesEuro08.add(new Waypoint(zuerich));
  10. hostCitiesEuro08.add(new Waypoint(basel));
  11. ...
  12.  
  13. WaypointPainter<JXMapViewer> painter =
  14.   new WaypointPainter<JXMapViewer>();
  15. painter.setWaypoints(hostCitiesEuro08);
  16. mapViewer.getMainMap().setOverlayPainter(painter);

The WaypointPainter is responsible for painting the markers for waypoints at the map. The default WaypointPainter draws a blue balloon at the given GeoPosition.

Changing the map provider
At the moment the SwingLabs library provides two implementations for map providers. The OpenStreetMap as we have seen in this example and NASA's BlueMarble. It is possible to write your own provider. How to do this i'll post in my next blog. So stay tuned and enjoy experimenting with the great JXMapKit component from SwingLabs.
Please feel free to download the complete source code and the needed libraries for the Euro2008 application: Source Code for the Euro2008 - Host Cities Viewer

2 comments June 16th, 2008


Calendar

June 2008
M T W T F S S
« Mar    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Posts by Month

Posts by Category