Skip to main content

Adding Google Maps to your Java Application

Posted by cajo on October 16, 2010 at 7:31 PM PDT

Google maps was a useful hit from the moment it went on line. Since then thousands of web pages have added map capability to their sites, courtesy of Google. Let me illustrate how you can add Google maps to your Java application.

Background

Google furnishes its maps via a simple REST request. Does this mean you need to add some fancy REST framework to your application? Not at all! Java provides all you need right in the standard libraries, and it is very easy to do. (that's part of the real elegance of REST)

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.

Want More?

Of course I'm not going to leave you hanging like that! ;-) I've written a complete free runnable GoogleMap Java component for you, it's less than one page of code. Try it out and let me know what you think.

Enjoy,

John

Comments

 I'm sorry, I couldn't find a link to the class you ...

I'm sorry, I couldn't find a link to the class you create it. The one in the post is broken.

Could you provide me with one please?

Thanks!

<p>Link to code is broken .....</p> <p>Hi John,</p> <p>The ...

Link to code is broken .....
Hi John,
The link to your example code is broke. I will much appreciate it if you can (re)post your code or fix the link. Thanks a ton!

&nbsp;Hi John seems like publishing CAJO project broke link ...

Hi John seems like publishing CAJO project broke link for this GoogleMap, can you please fix this link?

 Hi, with

Hi,

with JAK labs.micromata.de/display/jak/Home you can even add KML graphics from Java.

Thomas

Interesting, however Google

Interesting, however Google forbids you from using this API outside of the browser (unless you pay for the Premier API, about 10K$ a year)

Quote from the overview at http://code.google.com/intl/fr/apis/maps/documentation/staticmaps/
"Note that static maps may only be displayed within browser content; use of static maps outside of the browser is not allowed."

too bad.

forbids?... perhaps there is

forbids?... perhaps there is a much bigger world, outside the browser. Maybe we need another map source? ... sorry liquid.

Yes, unfortunately Google

Yes, unfortunately Google Maps are not open at all (Google is open only where it's open), the restrictions of use are well known... reading the title I hoped that they relaxed their terms of use. Too bad. OpenStreetMap provides really free maps (but I don't recall whether they provide a "static" image API, for sure they provide tiles). Even Microsoft is probably more open than Google in this field.

google can't know, and

google can't know, and ultimately what difference does it make, if the map is requested from a browser, or not? how much control does a corporation get... over people?

When I saw the phrase "the

When I saw the phrase "the full functionality of Google Maps" I was expecting, well, the full functionality of Google Maps. Like being able to zoom in, or change to push the map sideways so you could see more map. But all we have here is the ability to display a static copy of a particular Google Map view. No doubt this could be a useful tool. You just grossly oversold it, that's all.

Hi Paul, it's all there, just

Hi Paul, it's all there, just in raw form. Google simply uses javascript to re-request new maps based on user actions. For example it is quite easy to add a JSlider to change the zoom in the map request. Scrolling across maps could be handled similarly in a JScrollPane. Perhaps you are right, I should have mentioned that those things are left as an exercise for the reader. ;)

Hi John, Thank you for the

Hi John, Thank you for the nice little GoogleMap class :)

Though it's cool, the parameter for the constructor can be a bit hard to understand/read, so I started working on a builder for your class and it can be found at http://bit.ly/ciDyug - With credit to you of course.

Best regards
Lars Steen

PS. Some form for function that automate converts linebreaks to to the right html-code in the comment field would also be great.

Very nice Lars! :-) I always

Very nice Lars! :-)

I always enjoy the opportunity to see people contribute and enhance shared code.

My post was intended just as the seed, to demonstrate how to use the technology. So many interesting things can be done with it.

Warmest Regards,

John

<p>I have tryed but nofing happened. Plz help me someone ...

I have tryed but nofing happened. Plz help me someone <a href="http://www.mapmonde.org/">here</a>.