Today is Sunday, 5th August 2018

Archive for May, 2015

How to Add Google Maps to Java Swing GUI

One of our readers recently had a question regarding Google Maps and Java Swing. He wanted to know how to use Google Maps inside of a Java Swing GUI application.

There are a number of solution out there. In fact, StackOverflow has a good thread about the various solutions.

There is also a commercial product that provides support for GoogleMaps. The JxBrowser component from is a Swing GUI component that you can add to your application. It provides tight integration with Google Maps and has the same look and feel of the regular GoogleMaps application. The only downside is the product costs money ($$$). You can visit the teamdev website to see sample source code and also get pricing information.

However, if you are looking for a free solution, then you can get basic GoogleMaps support. Note that this is very bare bones … but hey, it is free 🙂

Here’s a simple example that will display a GoogleMap in a Swing application.

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GoogleMapsDemo {
public static void main(String[] args) throws IOException {
JFrame test = new JFrame("Google Maps");
try {
String latitude = "40.714728";
String longitude = "-73.998672";
String imageUrl = ""
+ latitude
+ ","
+ longitude
+ "&zoom=11&size=612x612&scale=2&maptype=roadmap";
String destinationFile = "image.jpg";
// read the map image from Google
// then save it to a local file: image.jpg
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = != -1) {
os.write(b, 0, length);
} catch (IOException e) {
// create a GUI component that loads the image: image.jpg
ImageIcon imageIcon = new ImageIcon((new ImageIcon("image.jpg"))
.getImage().getScaledInstance(630, 600,
test.add(new JLabel(imageIcon));
// show the GUI window


And here’s the output of the program.