JAVA Swing中的谷歌地图

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

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

我刚尝试了这个,它就像一个魅力:

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

实际上,在lp2_1背后是什么,如果你没有在你的面板上获得地图,这个控件可能就是问题所在。

将上面的回答中的String imageUrl替换为Recall

String imageUrl =“ https://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=11&size=612×612&scale=2&maptype=roadmap ”;

对于特定地理点(经度和纬度)的地图

可以轻松调整纬度,经度,缩放级别(0-21)和大小(0-612)