0

我使用OSMdroid Mapview和使用AsyncTask類來獲取一些數據,我創建覆蓋,並嘗試重新繪製每次我得到一個味精。MapView不會失效onProgressUpdated的異步任務

不幸的是,我能夠從客戶端獲取數據,並且我能夠在onProgressUpdated中創建覆蓋圖,我甚至稱其爲invalidate();但似乎沒有任何事情發生。不知道是什麼問題?

這裏是我的AsyncTask

public class TaskManager extends AsyncTask<Void, GeoPoint, Void>{ 

    ..... 

    public TaskManager(Master master,MapView mapview) { 
     //Construtor 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 

     if(Constance.TCPIP) { 
      Log.d("APP","Inside TCPIP"); 
      //Creation of TCPIP Sockets 
      try { 
       m_ssocket = new ServerSocket(Constance.PORT_NO); 
       Log.d("APP","ServerSocket: "+m_ssocket); 
       m_socket = m_ssocket.accept(); 
       Log.d("APP","Accepted: "+m_socket); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     else if (Constance.UDPIP) {   

      //Creation of UDP Sockets 
      try { 
       m_dsocket = new DatagramSocket(Constance.PORT_NO); 
      } catch (SocketException e) { 
       e.printStackTrace(); 
      } 
     } 
     else if (Constance.MCUDP) { 

      //Lock Wifi multicast 
      mMultiCastLock = new MultiCastLock(mMaster.getBaseContext()); 
      mMultiCastLock.setMultiCastAcquire(); 

      //Creation of MC-UDP Sockets 
      try { 
       m_mcsocket = new MulticastSocket(Constance.PORT_NO); 
       InetAddress address = InetAddress.getByName(Constance.GROUP_ADDR); 
       m_mcsocket.joinGroup(address); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     // Create a buffer to read datagrams into. 
     byte[] mSocketbuffer = new byte[Constance.DGRAM_LEN]; 

     if(Constance.TCPIP) { 
      try { 
       m_inSocketData = new BufferedReader(new InputStreamReader(m_socket.getInputStream())); 
       Log.d("APP","Reading"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } else {    
      // Create a packet to receive data into the buffer 
      m_inPacket = new DatagramPacket(mSocketbuffer, mSocketbuffer.length); 
     } 

     //prepare overlay items 
     prepareItemizedOverlay(); 

     // Now loop forever, waiting to receive packets and printing them. 
     if(m_ssocket!=null || m_dsocket!=null || m_mcsocket!=null) 
     while (true) { 

     if (isCancelled()) break; 

      //Get Data 
      parseData(); 

      //Make Packet Object 
      if(mMSG!=null) { 
       make(mMSG); 
      } 

      if(m_inPacket!=null && !Constance.TCPIP) { 
       // Reset the length of the packet before reusing it. 
       m_inPacket.setLength(mSocketbuffer.length); 
      } 
     } 


     return null; 
    } 

    @Override 
    protected void onProgressUpdate(GeoPoint... geoPoints){ 

    OverlayItem overlayItem = new OverlayItem("Name", "Description", geoPoints[0]); 
      mItemizedOverlay.addOverlay(overlayItem); 
      mMapView.getOverlays().add(mItemizedOverlay); 
      mMapView.getController().animateTo(geoPoints[0]); 

     mMapView.invalidate(); 

    } 

    @Override 
    protected void onCancelled() { 
     super.onCancelled(); 
     if(Constance.TCPIP) { 
      if(m_ssocket!=null && m_socket!=null){ 
       try { 
        m_ssocket.close(); 
        m_socket.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } else if(Constance.UDPIP) { 
      if(m_dsocket!=null) 
       m_dsocket.close(); 
     } else if(Constance.MCUDP) { 
      if(m_mcsocket!=null) 
       m_mcsocket.close(); 
     } 
     Log.d("APP","Task Ended"); 
    } 

    private void parseData() { 

     if(Constance.TCPIP) { 
      // Wait to receive a socket data 
      try{ 
       mMSG = m_inSocketData.readLine(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      // Wait to receive a datagram 
      try { 
       m_dsocket.receive(m_inPacket); 
       // Convert the contents to a string, and display them 
      } catch (IOException e) { 
       e.printStackTrace(); 
       } 
     }  
    } 

    private void make(String plot) { 


      //Make Object 
      mMSG = new MSG(plot); 

      //Overlay 
      mGeoPoint = mMSG.getGeoPoint(); 
      publishProgress(mMSG.getGeoPoint()); 

    } 

    private void prepareItemizedOverlay() { 
     /* itemized overlay */ 
     Drawable newMarker = mMaster.getResources().getDrawable(R.drawable.ic_sensor); 
     mItemizedOverlay = new PlotItemOverlay(mMaster,mItemList,newMarker, 
       new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() { 

       @Override 
       public boolean onItemSingleTapUp(int index, OverlayItem item) { 
       Log.d("APP","HERE"); 
       return true; 
       } 

       @Override 
       public boolean onItemLongPress(int index, OverlayItem item) { 

        return true; 
       } 
      }, mResourceProxy); 

    }  
} 

一切似乎工作,但似乎沒有發生,不知道是什麼問題?

+1

animateTo()調用是否工作?請注意,這將導致mapView失效,因爲它會導致滾動。 – kurtzmarc

+0

否animateTo()調用不起作用。我還觀察了一件事,如果我使用硬編碼值而不使用套接字,那麼mapView完美地顯示overlay和animateTo()調用。我不知道什麼時候我用套接字來做這件事情,它不工作。什麼似乎是問題? – zIronManBox

回答

0

最後解決了它。實際上,我正在替換導致所有這些Object丟失的MapFragment類,並且創建了一個新對象,以便與舊的MapFragment接口,而不是新的MapFragment。一旦我找到邏輯分析代碼,就解決了。無論如何,感謝支持@kurtzmarc你一直非常有幫助,直到現在。我將繼續與OSMdroid相同,以查看我提出的更多事情。