简体   繁体   English

在JMapViewer中动态更新标记

[英]Dynamically updating markers in JMapViewer

Hello Stack Overflow community, 您好Stack Overflow社区,

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. 我是Java新手,我正在做一个简单的Java项目,我从一个(动态)源中获取坐标(纬度和经度),并使用JMapViewer(是,不是JXMapViewer)在地图上显示标记。 I have put all the coordinates in two ArrayList(s). 我已将所有坐标放入两个ArrayList中。 It looks like that: 看起来像这样:

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

EDIT: map is a jMapViewer object. 编辑:地图是一个jMapViewer对象。

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: 问题是我需要使用计时器每20秒刷新一次地图,而我发现的唯一方法是关闭并打开地图,如下所示:

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

EDIT: 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)) 编辑:theMap是我在主函数中创建的一个对象(jFrame不是jMapViewer)(例如在演示中),并且我不能在其上使用addMapMarker(例如theMap.addMapMarker(150.2,150.2))

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). 很好,就像您可以想象的那样,这很烦人(每隔20秒,窗口就会关闭并打开,并且以前的“浏览”会话会丢失)。 So is there a way to refresh it? 有没有办法刷新它? By adding markers dynamically or just refreshing the content? 通过动态添加标记还是仅刷新内容?

Thanks a lot. 非常感谢。

I have never used that API but it looks like theMap.removeAllMapMarkers(); 我从未使用过该API,但看起来像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)));
}

I see two approaches: 我看到两种方法:

  • Maintain a collection of existing MapMarker instances and use removeMapMarker() followed by addMapMarker() using the immutable MapMarkerDot implementation provided. 维护现有的集合MapMarker实例和使用removeMapMarker()然后addMapMarker()使用不变MapMarkerDot提供的实现。 Both methods invoke repaint() . 两种方法都调用repaint()

  • Implement the MapMarker interface to create a MutableMapMarkerDot ; 实现MapMarker接口以创建MutableMapMarkerDot add as many instances as required; 根据需要添加尽可能多的实例; update the coordinates in situ and invoke repaint() in your javax.swing.Timer listener. 在原位更新坐标并在javax.swing.Timer侦听器中调用repaint()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM