Jay
Jay

Reputation: 2107

Update GoogleMap static image from Java Swing?

I can display the image in my swing app using:

double longitude = 10;
double lat = 20

I then print the image:

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

I've now changed it:

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

I'm then trying to refresh the map via calling map(); then repaint(); however it's still not working =[

Upvotes: 0

Views: 828

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

I had a play around and I don't seem to have any problems...

Fly me to the moon

(I did change the zoom level so I didn't end up with just "blue" all the time)

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

    }

}

Upvotes: 2

jlordo
jlordo

Reputation: 37813

I must guess you have something like this:

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

this will have no effect, because map and mapImage don't change. Try inserting following code where I have put the // here comment:

map = new URL("http://maps.google.com/staticmap?center="+longitude+","+lat +"&zoom=14&size=500x500");
mapImage = ImageIO.read(map);

Upvotes: 2

Related Questions