Over a million developers have joined DZone.

Display Static Google Maps in a Java Desktop Application

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

Displaying static Google Maps in a Java desktop application sounds complicated, since iGoogle Maps provide a JavaScript API that can be exploited from web pages based on a Google key per host. Besides this API, Google Maps also provides a URL based API for extracting static Google Maps directly into desktop application, without requiring JavaScript or access keys. This API is explained very well here: http://code.google.com/apis/maps/documentation/staticmaps/index.html.


Based on this tutorial, Java can extract and save a Google Maps static map in just a few lines of code, without any high complexity involved. Even though the API is very simple, it took me a while to get to this API, so I will share my code here:

try {
    BufferedImage img = ImageIO.read(new URL("http://maps.googleapis.com/maps/
    api/staticmap?sensor=false&size=512x512¢er=Brooklyn&zoom=12&style=feature:
    road.local%7Celement:geometry%7Chue:0x00ff00%7 Csaturation:100&style=feature:  
    landscape%7Celement:geometry%7Clightness:-100"));
    
    File outputfile = new File("map.png");
    ImageIO.write(img, "png", outputfile);
    System.out.println("Saved!");
    } catch (Exception ex) {
         System.out.println("Error!" + ex);
    }
The map below is taken from Google Maps, and it looks as follows:

 

From http://e-blog-java.blogspot.com/2011/12/display-static-google-maps-in-java.html

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}