Google Map in JAVA Swing

后端 未结 2 645
我在风中等你
我在风中等你 2020-12-05 08:53

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

相关标签:
2条回答
  • 2020-12-05 09:34

    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

    0 讨论(0)
  • 2020-12-05 09:36

    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.

    0 讨论(0)
提交回复
热议问题