Google地图在JAVA Swing中 [英] Google Map in JAVA Swing

查看:113
本文介绍了Google地图在JAVA Swing中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。没有编译错误,但我没有得到期望的输出:地图没有出现。
我想在我的JPanel中打开Goog​​le静态地图,并且想将其保存在本地驱动器上。这是我正在使用的代码。请指导我出错的地方。

 尝试{
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 =新网址(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);

byte [] b =新字节[2048];
int长度; ((length = is.read(b))!= -1){
os.write(b,0,length);


}

is.close();
os.close();
} catch(IOException e){
e.printStackTrace();
System.exit(1); $(new ImageIcon(image.jpg))。getImage()
.getScaledInstance(630,600, ));



解决方案

尝试了这一点,它像一个魅力:

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

尝试{
String imageUrl =http://maps.google.com/staticmap?center=40,26&zoom=1&size=150x112&maptype=satellite&key= ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&安培;格式= JPG;
String destinationFile =image.jpg;
String str = destinationFile;
网址url =新网址(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);

byte [] b =新字节[2048];
int长度; ((length = is.read(b))!= -1){
os.write(b,0,length);


}

is.close();
os.close();
} catch(IOException e){
e.printStackTrace();
System.exit(1); (new ImageIcon(image.jpg))。getImage()。getScaledInstance(630,600,
java.jbabc.com) .awt.Image.SCALE_SMOOTH))));

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


$ b $ / code $ / pre
$ b $ p lp2_1 实际上,如果你没有在你的面板上得到地图,这个控制可能是问题。


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

}

解决方案

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.

这篇关于Google地图在JAVA Swing中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆