我在Swing中使用JMapViewer创建地图。我在地图上有几个代表汽车的MapMarkerDots。我试图更新这些标记的位置,以便它们似乎在地图上行驶,但是它不能正常工作。我有一个“汽车”要遵循的坐标列表,但是会发生什么情况是位置已更新,但标记不会重新绘制,直到完成为止,这意味着标记将在初始位置和最终位置处绘制,而不是在两者之间的每一点。我正在使用的代码如下。任何想法为什么这可能会发生?用OpenStreetMaps移动地图标记JMapViewer
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();
这被称为鼠标点击事件。
考虑展示一个证明问题的SSCCE - 没有@Catalina提供了可能的最佳猜测:-) – kleopatra 2013-03-01 12:45:25
什么是SSCCEE?我正在尝试@Catalina的建议,所以我们会看看我如何继续。 – Riddle 2013-03-03 10:18:44