12

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)));
Neuron
  • 5,141
  • 5
  • 38
  • 59
abhilash_goyal
  • 711
  • 1
  • 10
  • 31

2 Answers2

16

I just tried out this, and it worked like a charm:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Snippet {
    public static void main(String[] args) throws IOException {
        JFrame test = new JFrame("Google Maps");

        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";
            String 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);
        }

        test.add(new JLabel(new ImageIcon((new ImageIcon("image.jpg")).getImage().getScaledInstance(630, 600,
                java.awt.Image.SCALE_SMOOTH))));

        test.setVisible(true);
        test.pack();

    }
}

Whats behind lp2_1 actually, if you dont get the map on you panel, this control could be the problem.

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
  • I recently used the code given above. I did not understand the line where the ImageIcon is added to JFrame - test.add(new JLabel...). What is the point of creating an ImageIcon object and using it inside another ImageIcon, which is ultimately passed to JLabel. I get an error when I call the ImageIcon constructor inside JLabel directly. – Sarvavyapi Oct 28 '13 at 22:02
  • Thanks for the ready to use code! You can delete the "String str = destinationFile;" it is never used. – Ric Jafe Jan 10 '14 at 17:24
2

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

The Digital Ad Venture
  • 1,576
  • 1
  • 16
  • 24