0

我可以使用以下方法在我的摇摆应用程序中显示图像:

double longitude = 10;
double lat = 20

然后我打印图像:

JFrame frame = new JFrame();
frame.add(mapImage);

我现在改变了它:

public Image map() {
URL map = new URL("http://maps.google.com/staticmap?center="+long+","+longitude +"&zoom=14&size=500x500");
Image map = ImageIO.read((map));
return map;
}

JFrame frame = new JFrame();
frame.add(map);

然后我试图通过调用 map(); 来刷新地图。然后重绘();但是它仍然无法正常工作=[

4

2 回答 2

2

我玩过,好像没啥问题。。。

带我去月球

(我确实改变了缩放级别,所以我并没有一直只得到“蓝色”)

public class TestGoogleMaps {

    public static void main(String[] args) {
        new TestGoogleMaps();
    }

    public TestGoogleMaps() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                final MapPane mapPane = new MapPane();
                JButton random = new JButton("Random");
                random.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String lat = Double.toString((Math.random() * 180) - 90);
                        String longt = Double.toString((Math.random() * 360) - 180);
                        new LoadMapTask((JButton)e.getSource(), mapPane, lat, longt).execute();
                        ((JButton)e.getSource()).setEnabled(false);
                    }
                });

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(mapPane);
                frame.add(random, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class MapPane extends JPanel {

        private BufferedImage mapImage;
        private String latitude;
        private String longitude;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }

        public void setMap(String lat, String log, BufferedImage image) {
            mapImage = image;
            latitude = lat;
            longitude = log;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (mapImage != null) {
                int x = (getWidth() - mapImage.getWidth()) / 2;
                int y = (getHeight() - mapImage.getHeight()) / 2;
                g.drawImage(mapImage, x, y, this);
                FontMetrics fm = g.getFontMetrics();
                g.drawString(latitude + "x" + longitude, 0, fm.getAscent());
            }
        }

    }

    public class LoadMapTask extends SwingWorker<BufferedImage, Object> {

        private String latitude;
        private String longitude;
        private MapPane mapPane;
        private JButton master;

        public LoadMapTask(JButton master, MapPane mapPane, String latitude, String lonitude) {
            this.mapPane = mapPane;
            this.latitude = latitude;
            this.longitude = lonitude;
            this.master = master;
        }

        @Override
        protected BufferedImage doInBackground() throws Exception {
            BufferedImage mapImage = null;
            try {
                URL map = new URL("http://maps.google.com/staticmap?center=" + latitude + "," + longitude + "&zoom=5&size=500x500");
                System.out.println(map);
                mapImage = ImageIO.read(map);
            } catch (Exception exp) {
                exp.printStackTrace();
            }
            return mapImage;
        }

        @Override
        protected void done() {
            try {
                mapPane.setMap(latitude, longitude, get());
            } catch (InterruptedException | ExecutionException ex) {
                ex.printStackTrace();
            }
            master.setEnabled(true);
        }

    }

}
于 2012-11-25T06:22:30.923 回答
2

我一定猜你有这样的事情:

longitude = newLongitude;
lat = newLatitude;
// here
repaint();

这不会有任何影响,因为map并且mapImage不会改变。尝试在我// here发表评论的地方插入以下代码:

map = new URL("http://maps.google.com/staticmap?center="+longitude+","+lat +"&zoom=14&size=500x500");
mapImage = ImageIO.read(map);
于 2012-11-25T00:34:19.037 回答