Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JavaSwing更新GoogleMap静态图像?_Java_Swing_Google Maps - Fatal编程技术网

从JavaSwing更新GoogleMap静态图像?

从JavaSwing更新GoogleMap静态图像?,java,swing,google-maps,Java,Swing,Google Maps,我可以使用以下方式在swing应用程序中显示图像: 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

我可以使用以下方式在swing应用程序中显示图像:

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()刷新映射;然后重新绘制();但是它仍然不起作用=[/p>我猜你有这样的东西:

longitude = newLongitude;
lat = newLatitude;
// here
repaint();
这将无效,因为
map
mapImage
不会更改。请尝试在我放置
//此处
注释的位置插入以下代码:

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

我猜你一定有这样的想法:

longitude = newLongitude;
lat = newLatitude;
// here
repaint();
这将无效,因为
map
mapImage
不会更改。请尝试在我放置
//此处
注释的位置插入以下代码:

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

我有一个游戏,我似乎没有任何问题

(我确实改变了缩放级别,所以我没有一直以“蓝色”结束)

公共类TestGoogleMaps{
公共静态void main(字符串[]args){
新的TestGoogleMaps();
}
公共测试GoogleMaps(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
}catch(实例化异常){
}捕获(非法访问例外){
}捕获(无支持的LookandFeelexception ex){
}
最终地图窗格地图窗格=新地图窗格();
JButton random=新JButton(“随机”);
random.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
字符串lat=Double.toString((Math.random()*180)-90);
字符串长度=Double.toString((Math.random()*360)-180);
新建LoadMapTask((JButton)e.getSource(),mapPane,lat,lont).execute();
((JButton)e.getSource()).setEnabled(false);
}
});
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(地图窗格);
框架。添加(随机,边界布局。南部);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类映射窗格扩展了JPanel{
私有缓冲区映像映射映像;
私有字符串纬度;
私有字符串经度;
@凌驾
公共维度getPreferredSize(){
返回新维度(500500);
}
公共void setMap(字符串lat、字符串日志、缓冲区图像){
mapImage=图像;
纬度=纬度;
经度=对数;
重新油漆();
}
@凌驾
受保护组件(图形g){
超级组件(g);
如果(mapImage!=null){
intx=(getWidth()-mapImage.getWidth())/2;
int y=(getHeight()-mapImage.getHeight())/2;
g、 drawImage(mapImage,x,y,this);
FontMetrics fm=g.getFontMetrics();
g、 拉丝(纬度+x+经度,0,fm.getAscent());
}
}
}
公共类LoadMapTask扩展SwingWorker{
私有字符串纬度;
私有字符串经度;
私人地图窗格地图窗格;
私人按钮主控器;
公共LoadMapTask(JButton主控、MapPane MapPane、字符串纬度、字符串纬度){
this.mapPane=mapPane;
这个。纬度=纬度;
这是经度;
this.master=master;
}
@凌驾
受保护的BuffereImage doInBackground()引发异常{
BuffereImage mapImage=null;
试一试{
URL映射=新URL(“http://maps.google.com/staticmap?center=“+纬度+”、“+经度+”&zoom=5&size=500x500”);
系统输出打印项次(map);
mapImage=ImageIO.read(映射);
}捕获(异常扩展){
exp.printStackTrace();
}
返回mapImage;
}
@凌驾
受保护的void done(){
试一试{
setMap(纬度、经度、get());
}捕获(InterruptedException | ExecutionException ex){
例如printStackTrace();
}
master.setEnabled(真);
}
}
}

我玩了一场,似乎没有任何问题

(我确实改变了缩放级别,所以我没有一直以“蓝色”结束)

公共类TestGoogleMaps{
公共静态void main(字符串[]args){
新的TestGoogleMaps();
}
公共测试GoogleMaps(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(ClassNotFoundException ex){
}catch(实例化异常){
}捕获(非法访问例外){
}捕获(无支持的LookandFeelexception ex){
}
最终地图窗格地图窗格=新地图窗格();
JButton random=新JButton(“随机”);
random.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
字符串lat=Double.toString((Math.random()*180)-90);
字符串长度=Double.toString((Math.random()*360)-180);
新建LoadMapTask((JButton)e.getSou