2013-03-01 45 views
0

我在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(); 

這被稱爲鼠標點擊事件。

+0

考慮展示一個證明問題的SSCCE - 沒有@Catalina提供了可能的最佳猜測:-) – kleopatra 2013-03-01 12:45:25

+0

什麼是SSCCEE?我正在嘗試@Catalina的建議,所以我們會看看我如何繼續。 – Riddle 2013-03-03 10:18:44

回答

2

Daemon聽起來像後臺線程,所以你需要在event dispatch thread(EDT)012ff上做任何更新。如果可行,SwingWorker可能是一個好方法,可以使Daemon在工作人員的process方法中執行常規EDT更新。

+0

這有效,但它使我的CPU使用率達到100%,我得到這個異常'java.util.ConcurrentModificationException'。你有什麼想法,爲什麼這可能是?我以前從未使用過SwingWorker,所以我不確定我擁有的是否可以。我對我的問題所作的編輯看起來好嗎? – Riddle 2013-03-03 11:03:04

+1

@Riddle你沒有顯示的代碼有問題(_SSCCE_ - 谷歌是你的朋友找到鏈接,這裏沒有它的方便) – kleopatra 2013-03-03 11:31:40

+1

你絕對不應該在'doInBackground'中繪製; '發佈'你的新觀點並畫'過程'。 – 2013-03-04 12:20:55