SwingLabs JXMapKit and Google Maps
June 30th, 2008 Mario Hochreiter
As promised in my last blog entry Map Application in Swing - The Easy Way in this short blog entry i would like to explain how to change the default map providers for the JXMapKit component from the SwingX-WS project.
The sample application still has the same functionality as in my first blog entry - to simply show a map in which the host cities of the Euro2008 are marked but this time using the Google Maps Tile server as provider for the map tiles. This is how the resulting application will look like:
![]()
Some (un)necessary info before...
The Google Maps Tile server can be reached at following address: http://mt.google.com.
This server is responsible for delivering the Google Maps tiles, please take a deeper look into the Google Maps API to understand how Google Map Tiles are addressed (Google Map API Coordinates). To get a tile from the Google Maps server some additional parameter are necessary for to add to the http://mt.google.com url. First we need an x and y coordinate as well as the zoom level to address a specific tile. The parameters v and n are needed for to satisfy the Google server. For example the following url:
http://mt.google.com/mt?n=404&v=w2.75&x=8937&y=5681&zoom=3 delivers a tile from the inner city of vienna at zoom level 3.
... we finally can implement our own Map Provider
A TileFactoryInfo encapsulates all information specific to a map server. This includes everything from the url to load the map tiles from to the size and depth of the tiles. Theoretically any map server can be used by installing a customized TileFactoryInfo. That's what the API of the SwingX-WS project says about the TileFactoryInfo class and that's also exactly what we need ![]()
The only thing we need to do is to construct a TileFactoryInfo object with the correct parameters for the Google Maps Tile server.
-
public class GoogleMapsTileProvider {
-
private static final int minZoom = 1;
-
private static final int maxZoom = 16;
-
private static final int mapZoom = 17;
-
private static final int tileSize = 256;
-
private static final boolean xr2l = true;
-
private static final boolean yt2b = true;
-
"http://mt1.google.com/mt?n=404&v=w" + VERSION;
-
-
private static final TileFactoryInfo
-
GOOGLE_MAPS_TILE_INFO = new TileFactoryInfo(
-
minZoom, maxZoom, mapZoom, tileSize, xr2l,
-
yt2b, baseURL, x, y, z);
-
-
public static TileFactory getDefaultTileFactory()
-
{
-
return (new DefaultTileFactory(
-
GOOGLE_MAPS_TILE_INFO));
-
}
-
}
The minimal possible zoom level is 1, the maximum possible zoom level is 16, the tile size is 256x256 pixel, the x axis goes from left to right, and the y axis from top to bottom.
The following code sets the tile factory to our own implementation.
-
JXMapKit mapViewer = new JXMapKit();
-
mapViewer.setTileFactory(
-
GoogleMapsTileProvider.getDefaultTileFactory());
Voila, from now on you can use the Google Maps Tiles in your own images but please note that your are using a service from Google without having signed their Terms of Use. So this guide is more for scientific purposes than to use in real world applications.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed