2013-03-01 87 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

相关问题