java - JAVA Swing 中的谷歌地图

标签 java google-maps

这是我的代码。没有编译错误,但我没有得到所需的输出: map 没有出现。 我想在我的 JPanel 中打开 Google 静态 map ,还想将它保存在我的本地驱动器上。这是我正在使用的代码。请指导我哪里出错了。

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)));

最佳答案

我刚试过这个,效果非常好:

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();

    }
}

lp2_1 的背后实际上,如果您没有在面板上获取 map ,则可能是此控件有问题。

关于java - JAVA Swing 中的谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17598074/

相关文章:

java - 将列表放入sharedPreferences中

json - 无法将 JSON 过滤器应用于 Google map 中的美洲原住民保留区

android - 如何在谷歌地图 Android 上显示多个标记?

javascript - Google map API - 将鼠标悬停在标记上并显示图像

javascript - 在 IE8 中打印谷歌地图打印和使用 'page-break-before' 时出现问题

android - Google Maps v2 上的动画标记

java - 无效的 java 正则表达式转义序列未在 Eclipse 中标记

java - Apache Camel Rest 对第一个请求的响应时间较长

java - 使java项目与maven集成

java - 如何制作使用每个字段等于而不是默认等于的java接口(interface)/抽象类?