Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map in JAVA Swing

Here is my code. There are no compilation errors, but I am not getting desired output: the map is not appearing. I want to open Google static map in my JPanel and also want to save it on my local drive. This is the code which I am using. Kindly guide where I am going wrong.

try {
    String imageUrl =
            "http://maps.google.com/staticmap?center=40,26&zoom=1&size=150x112&maptype=satellite&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg";
    String destinationFile = "image.jpg";
    str = destinationFile;
    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
} catch (IOException e) {
    e.printStackTrace();
    System.exit(1);
}
lp2_1.setIcon(new ImageIcon((new ImageIcon("image.jpg")).getImage()
        .getScaledInstance(630, 600, java.awt.Image.SCALE_SMOOTH)));
like image 641
abhilash_goyal Avatar asked Jul 11 '13 15:07

abhilash_goyal


People also ask

How to display Google map in Java?

To embed Google Maps in your Java application you need to create the map. html file that initializes and displays the map, create and embed web browser component, load the map. html file and communicate with the loaded map using JxBrowser API and Google Maps JavaScript API.


1 Answers

Replace the String imageUrl from the answer above from Recall to

String imageUrl = "https://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=11&size=612x612&scale=2&maptype=roadmap";

for a Map of a specific Geographic point(latitude and longitude)

latitude, longitude, zoom level (0-21) and size (0-612) can be easily adapted

like image 167
The Digital Ad Venture Avatar answered Sep 18 '22 18:09

The Digital Ad Venture