Java 使用OpenStreetMaps JMapViewer移动地图标记
我正在Swing中使用JMapViewer创建地图。我在地图上有几个代表汽车的地图标记点。我正在尝试更新这些标记的位置,使它们看起来像是在地图周围行驶,但是它不能正常工作。我有一个“汽车”将遵循的坐标列表,但发生的是位置被更新,但标记在完成之前不会被重新绘制,这意味着标记在初始位置和最终位置绘制,而不是在两者之间的每一点。下面是我使用的代码。你知道为什么会这样吗Java 使用OpenStreetMaps JMapViewer移动地图标记,java,swing,map,openstreetmap,jmapviewer,Java,Swing,Map,Openstreetmap,Jmapviewer,我正在Swing中使用JMapViewer创建地图。我在地图上有几个代表汽车的地图标记点。我正在尝试更新这些标记的位置,使它们看起来像是在地图周围行驶,但是它不能正常工作。我有一个“汽车”将遵循的坐标列表,但发生的是位置被更新,但标记在完成之前不会被重新绘制,这意味着标记在初始位置和最终位置绘制,而不是在两者之间的每一点。下面是我使用的代码。你知道为什么会这样吗 public void drawRoute(String id){ MapMarkerDot mmd;
public void drawRoute(String id){
MapMarkerDot mmd;
String evMarkerObject; // ID and Marker position
String[] items, locations;
double lat, lon;
for(int i = 0; i < route.length-1; i+=2){ // Iterate through the route
List markers = zmap.getMapMarkerList(); // Get the markers that are currently on the map
for(int j = 0; j < Daemon.evMarkers.size(); j++){ // Go through the list of recorded marker IDs and locations
evMarkerObject = Arrays.toString(Daemon.evMarkers.get(j)); // Get marker id and location
items = evMarkerObject.split(", "); // Split the ID and location
if(items[0].substring(1).equals(id)){ // If an ID match is found
locations = items[1].split(" "); // Split location values by " "
lat = Double.parseDouble(locations[2]); // Get latitude of marker
lon = Double.parseDouble(locations[3]); // Get longitude of marker
for(int k = 0; k < markers.size(); k++){ // Go through list of markers currently on map
mmd = (MapMarkerDot) markers.get(k); // Get each marker in turn
if((mmd.getLat() == lat) && (mmd.getLon() == lon)){ // Check if recorded position matches marker position
zmap.removeMapMarker(mmd); // Remove marker from the map
break; // Break from loop (appropriate marker found)
}
}
Daemon.evMarkers.remove(j); // Remove record of marker ID and position
zaddMarker(Color.BLUE, route[i], route[i+1], 'e', items[0].substring(1)); // Add marker at new position
//zmap.repaint();
}
}
}
public void drawRoute(字符串id){
mapmarkerdotmd;
字符串evMarkerObject;//ID和标记位置
字符串[]项、位置;
双lat,lon;
对于(inti=0;i
调用函数(基于@Catalina的回答):
SwingWorker-worker=新的SwingWorker(){
@凌驾
受保护的Void doInBackground()引发异常{
提取路线(markerID);
返回null;
}
};
worker.execute();
这是在鼠标单击事件时调用的。守护进程听起来像是后台线程,因此您需要在(EDT)上执行任何更新使用
SwingUtilities.invokeLater
。如果这样做有效,可能是让您的守护进程
在工作者的进程
方法中定期执行EDT更新的一个好方法。考虑显示一个演示问题的SSCCE-如果没有@Catalina,则提供可能的最佳猜测:-)SSCCE是什么?我正在尝试@Catalina建议的方法所以我们来看看我是怎么做的。虽然这样做可以让我的CPU使用率达到100%,但我得到了这个异常java.util.ConcurrentModificationException
。你知道为什么会这样吗?我以前从未使用过SwingWorker,所以我不确定我所做的是否正确。我对我的问题所做的编辑看起来正常吗?@Riddle有什么问题吗对于您没有显示的代码(SSCCE-谷歌是您查找链接的朋友,这里没有),您绝对不应该在doInBackground
;发布您的新观点并在过程中绘制。
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){
@Override
protected Void doInBackground() throws Exception {
drawRoute(markerID);
return null;
}
};
worker.execute();