【问题标题】:Google Map in JAVA SwingJAVA Swing 中的谷歌地图
【发布时间】:2013-07-10 00:03:05
【问题描述】:

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

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

【问题讨论】:

  • @david99world:我是新手,所以对好的文档不太了解,谢谢编辑...

标签: java google-maps


【解决方案1】:

我刚刚试了一下,效果很好:

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 后面的内容实际上是什么,如果您没有在面板上看到地图,则此控件可能是问题所在。

【讨论】:

  • @AbhilashGoyal 对你有帮助吗?
  • 我最近使用了上面给出的代码。我不明白 ImageIcon 添加到 JFrame 的行 - test.add(new JLabel ...)。创建一个 ImageIcon 对象并在另一个 ImageIcon 中使用它有什么意义,最终传递给 JLabel。直接在 JLabel 中调用 ImageIcon 构造函数时出现错误。
  • 感谢您准备好使用的代码!您可以删除“String str = destinationFile;”它从未使用过。
【解决方案2】:

将上面答案中的字符串 imageUrl 从 Recall 替换为

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

用于特定地理点的地图(纬度和经度)

纬度、经度、缩放级别 (0-21) 和大小 (0-612) 可以轻松调整

【讨论】:

    猜你喜欢
    • 2011-02-13
    • 2010-11-07
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    相关资源
    最近更新 更多