2012-11-25 59 views
0

我可以在我的揮杆應用程序中使用顯示圖像:從Java Swing更新GoogleMap靜態圖像?

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()來刷新地圖;然後重繪();但它仍然沒有工作= [

+0

向我們展示了您是如何嘗試去做的。 – jlordo

+0

如果你的'longitude'和'lat'變量改變了,'map'和'mapImage'變量什麼都沒有發生,所以'repaint()'不會顯示任何不同的東西。 – jlordo

+0

顯示你的'map()'調用,或更多的相關代碼。你如何處理'map()'的返回值? – jlordo

回答

2

我必須想你有這樣的事情:

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

這不會有任何影響,因爲mapmapImage不會改變。嘗試插入下面的代碼,我已經把// here評論:

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

對不起,我只是把你的評論考慮在內,並試圖改變我的代碼。 – Jay

+0

仍然不起作用恐怕= [ – Jay

+1

@ user1203297:如果您更改了代碼並且它不起作用,則應該將最新代碼顯示爲原始問題的附錄,以便我們可以看到原因。 –

2

我周圍有一齣戲,我似乎沒有任何問題......

Fly me to the moon

(我沒有改變縮放級別,所以我並沒有總是以「藍色」結束)

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

    } 

}