2013-07-11 33 views
8

İşte benim kodum. Derleme hataları yok, ancak istenen çıktıyı elde edemiyorum: harita görünmüyor. Google statik haritayı JPanel'imde açmak ve aynı zamanda yerel sürücüme kaydetmek istiyorum. Bu kullanıyorum koddur. Lütfen yanlış gittiğim bir rehber.Google Map in JAVA Swing

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

} 
+0

uyarlanmış I Bunun için yeni, bu yüzden iyi dokümantasyon hakkında çok şey bilmiyorum, düzenleme için teşekkürler ... –

cevap

15

Sadece bu denedik ve bir cazibe gibi çalıştı: Eğer size panelde haritayı alamazsanız

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

    } 
} 

nedir aslında lp2_1 arkasında, bu kontrol problem olabilir.

+0

@AbhilashGoyal bu size yardımcı oldu mu? –

+0

Yakın zamanda yukarıda verilen kodu kullandım. ImageIcon'un JFrame - test.add (yeni JLabel ...) 'a eklendiği çizgiyi anlamadım. Bir ImageIcon nesnesi yaratma ve onu başka bir ImageIcon içinde kullanma, nihayetinde JLabel'e geçme noktası nedir? JLabel içinde doğrudan ImageIcon yapıcıyı çağırdığımda bir hata alıyorum. – Sarvavyapi

+0

Kullanıma hazır kod için teşekkürler! "String str = destinationFile;" öğesini silebilirsiniz. asla kullanılmaz. –