Adding Google Maps to your Java Application

Now you can get business locations, maps and directions while you're on the go. And it's all free.


Cruising around looking for a nearby coffee shop? Driving to that new restaurant but can't remember which street to turn right on? Now you can get business locations, maps and directions while you're on the go. And it's all free.


Real-time traffic

See where the congestion is, and estimate delays in over 30 major US metropolitan areas.


Detailed directions

Whether you plan to walk or drive, your route is displayed on the map itself, together with step-by-step directions.

Integrated search results
Local business locations and contact information appear all in one place, integrated on your map.


Easily movable maps
Interactive maps let you zoom in or out, and move in all directions so you can orient yourself visually.


Satellite imagery
Get a bird's eye view of your desired location.

Getting It
To request a map, you start with the following URL:
http://maps.google.com/maps/api/staticmap?

After the question mark, append all of the details you wish to be included in the map, separated by ampersand (&) symbols. For example:
http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&
zoom=14&size=512x512&maptype=roadmap
This requests a road map centering on the Brooklyn Bridge, in New York City, at zoom level 14, 512x512 pixels in size. There's a very rich collection of options for specifying and decorating maps, Google has very helpfully made a highly detailed page explaining them all.
As I hope you've reasonably guessed; the http request dutifully returns an image of the map requested. That's all there is to it!
Using It

Happily, Java provides all the resources you need to use this great Google feature right in the Standard Edition. Simply create a java.net.URLConnection, request the content, and generate the map image. If you've not done this before, fear not, as Java makes it very easy to do, it looks a bit like this:
URLConnection con = new URL("http://maps...").openConnection();
InputStream is = con.getInputStream();
byte bytes[] = new byte[con.getContentLength()];
is.read(bytes);
is.close();
Toolkit tk = getToolkit();
map = tk.createImage(bytes);
tk.prepareImage(map, -1, -1, null);
The variable map now has the image, ready for presentation! Remarkably easy wasn't it? That's all it takes, and you can enjoy the full functionality of Google maps in any Java application.