我在 Swing 中使用 JMapViewer 创建地图。我在地图上有几个代表汽车的 MapMarkerDots。我正在尝试更新这些标记的位置,以便它们看起来像是在地图上行驶,但它不能正常工作。我有一个“汽车”要遵循的坐标列表,但发生的情况是位置已更新,但标记在完成之前不会重绘,这意味着标记是在初始位置和最终位置绘制的,而不是在两者之间的每一点。我用于此的代码如下。关于为什么会发生这种情况的任何想法?
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();
}
}
}
调用函数(基于@Catalina 的回答):
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){
@Override
protected Void doInBackground() throws Exception {
drawRoute(markerID);
return null;
}
};
worker.execute();
这是在鼠标单击事件上调用的。