2015-11-05 38 views
1

我試圖執行動畫來模擬地圖上的路線,我必須顯示帶標記的路徑,逐個放置,等待X時間放入下一個。首先,我使用線程等待放置下一個標記,但它不會在任何設備上運行,並在運行時在地圖上發生中斷,因此會導致用戶體驗如此糟糕。然後,我決定使用animateCamera (Android API reference here)的回調功能來避免中斷,但我無法找到某種方式相互制作動畫,標記和位置的數量未確定,因此我必須重複。這是一些最後的想法代碼:通過放置標記來動畫路徑的方法

hashMarkers.get("Initial").setVisible(true); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(16), 5000, new GoogleMap.CancelableCallback() { 
      @Override 
      public void onFinish() { 
       for (COUNT=1; COUNT < hashMarkers.size() - 1;){ 
        CameraPosition cameraPosition = new CameraPosition.Builder() 
          .target(hashMarkers.get(String.valueOf(COUNT)).getPosition()) // Sets the center of the map to 
          .zoom(16)     // Sets the zoom 
          .bearing(-bearing)    // Sets the orientation of the camera to east 
          .tilt(90)     // Sets the tilt of the camera to 30 degrees 
          .build();     // Creates a CameraPosition from the builder 
        hashMarkers.get(String.valueOf(COUNT)).setVisible(true); 
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 3000, new GoogleMap.CancelableCallback() { 
         @Override 
         public void onFinish() { 
          COUNT++; 
         } 

         @Override 
         public void onCancel() { 

         } 
        }); 

此代碼只顯示初始標記和散列表中的第一個標記。

非常感謝您的任何貢獻,可能有助於解決此問題!

回答

1

我找到了解決方案,創建了可取消的回調,它對我有用,並可能對其他人有所幫助。

mMap.animateCamera(CameraUpdateFactory.zoomTo(16), 5000, new GoogleMap.CancelableCallback() { 
     @Override 
     public void onFinish() { 
      mMap.animateCamera(CameraUpdateFactory.zoomTo(16), 3000, myCallBack); 
     } 

     @Override 
     public void onCancel() { 

     } 
    }); 
} 

GoogleMap.CancelableCallback myCallBack = new GoogleMap.CancelableCallback() { 
@Override 
public void onFinish() { 
    if(++COUNT < hashMarkers.size()){ 

     CameraPosition cameraPosition = 
       new CameraPosition.Builder() 
         .target(hashMarkers.get(String.valueOf(COUNT)).getPosition()) 
         .tilt(90) 
         .bearing(-bearing) 
         .zoom(16) 
         .build(); 

     hashMarkers.get(String.valueOf(COUNT)).setVisible(true); 

     mMap.animateCamera(
       CameraUpdateFactory.newCameraPosition(cameraPosition), 
       3000, 
       COUNT == hashMarkers.size() - 1 ? FinalCancelableCallback : myCallBack); 
    } 
} 

@Override 
public void onCancel() { 
} 
}; 

GoogleMap.CancelableCallback FinalCancelableCallback =新GoogleMap.CancelableCallback(){

@Override 
public void onFinish() { 

    CameraPosition cameraPosition2 = new CameraPosition.Builder() 
      .target(hashMarkers.get("Final").getPosition()) // Sets the center of the map to 
      .zoom(17)     // Sets the zoom 
      .bearing(-bearing)    // Sets the orientation of the camera to east 
      .tilt(90)     // Sets the tilt of the camera to 30 degrees 
      .build();     // Creates a CameraPosition from the builder 
    hashMarkers.get("Final").setVisible(true); 
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition2), 3000, new GoogleMap.CancelableCallback() { 
     @Override 
     public void onFinish() { 
      allView = new LatLngBounds(
        allLatLng.get(0), allLatLng.get(allLatLng.size() - 1)); 
      mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(allView, 80), 2000, null); 
     } 

     @Override 
     public void onCancel() { 

     } 
    }); 

} 

@Override 
public void onCancel() { 
} 

};