在JMapViewer中动态更新标记 [英] Dynamically updating markers in JMapViewer

查看:84
本文介绍了在JMapViewer中动态更新标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Stack Overflow社区,

Hello Stack Overflow community,

我是Java新手,我正在做一个简单的Java项目,我从(动态)源中获取坐标(纬度和经度),并使用JMapViewer(是,不是JXMapViewer)在地图上显示标记.我已将所有坐标放入两个ArrayList中. 看起来像这样:

I am a Java newbie and I am doing a simple java project where I take coordinates (lat and lon) from a (dynamic) source and use JMapViewer (Yes, not JXMapViewer) to display the markers on a map. I have put all the coordinates in two ArrayList(s). It looks like that:

for(int i = 0; i < latArrayList.size(); i++){
    map.addMapMarker(new MapMarkerDot((double)latArrayList.get(i), (double)longArrayList.get(i)));
}

地图是一个jMapViewer对象.

map is a jMapViewer object.

它工作得很好. 问题是我需要使用计时器每20秒刷新一次地图,而我发现的唯一方法是关闭并打开地图,如下所示:

And it works pretty fine. The problem is I need this map to refresh every 20 seconds using a Timer and the only way I found was to close and open the map, like this:

    theMap.setVisible(false);
    theMap  = new Map();
    theMap.setVisible(true); 

theMap是我在主函数中创建的一个对象(jFrame不是jMapViewer)(就像在演示中一样),并且我不能在其上使用addMapMarker(例如theMap.addMapMarker(150.2,150.2))

theMap is an object (jFrame not jMapViewer) I create in the main function (like in the demo) and I can't use addMapMarker on it (like theMap.addMapMarker(150.2,150.2))

而且,正如您可以想象的那样,这很烦人(每隔20秒,窗口就会关闭并打开,并且以前的浏览"会话会丢失).那么有什么办法可以刷新它吗?通过动态添加标记还是只是刷新内容?

and well, as you can imagine this is pretty annoying (every 20 seconds the window closes and opens and the previous 'browsing' session is lost). So is there a way to refresh it? By adding markers dynamically or just refreshing the content?

非常感谢.

推荐答案

我从未使用过该API,但看起来theMap.removeAllMapMarkers();可以解决问题.然后,您可以再次开始添加新标记.

I have never used that API but it looks like theMap.removeAllMapMarkers(); would do the trick. You can then start adding new markers again.

关于循环,如果您使用泛型声明了列表,则无需将其强制转换为两倍:

Regarding your loop, if you declared your Lists with generics you would not need to cast to double:

List<Double> latArrayList = new ArrayList<Double> ();
latArrayList.add(125.87); //or whatever

for(int i = 0; i < latArrayList.size(); i++){
    theMap.addMapMarker(new MapMarkerDot(latArrayList.get(i), longArrayList.get(i)));
}

这篇关于在JMapViewer中动态更新标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆