Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me!

Sunday, December 11, 2011

Display a Static Google Maps in a Java Desktop Application


Displaying a static Google Maps in a Java desktop application sound like something complicated, since is known that Google Maps provide a JavaScript API that can be exploit from web pages based on a Google key dedicated per host. Well, beside this API, Google Maps also provides an URL based API for extracting static Google Maps directly into desktop application, without requiring JavaScript or access keys. This API is very well explained 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 code lines, without high complexity involved – even if it is very simple it tooked me a while to get to this API, this is why I think to share this 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 like below:

No comments: