我在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