2013-01-19 93 views
2

我想在谷歌地圖加載(地圖已填充)之後做些什麼來實現呢?安卓谷歌地圖V2完成加載事件或回調

+0

我對此也感興趣。 AFAIK API文檔中沒有任何內容適合它。 ''''''''''''''''''''''''''''''''''''' }); '''用於檢查「..地圖是否有大小」。我嘗試過玩這個,並沒有成功。 – qubz

回答

0

qubz所述,ViewTreeObserver可用於在地圖加載完成後實現回調,因此用戶將獲得例如啓動後的正確位置:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // This is a small hack to enable a onMapLoadingCompleted-functionality to the user. 
    final View mapView = getSupportFragmentManager().findFragmentById(R.id.google_map_fragment).getView(); 
    if (mapView.getViewTreeObserver().isAlive()) { 
     mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @SuppressWarnings("deprecation") 
      // We use the new method when supported 
      @SuppressLint("NewApi") 
      // We check which build version we are using. 
      @Override 
      public void onGlobalLayout() { 
       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
        mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       } else { 
        mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
       } 
       // Send notification that map-loading has completed. 
       onMapFinishedLoading(); 
      } 
     }); 
    } 
} 

protected void onMapFinishedLoading() { 
    // Do whatever you want to do. Map has completed loading at this point. 
    Log.i(TAG, "Map finished loading."); 
    GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.google_map_fragment)) 
        .getMap(); 
    mMap.moveCamera(CameraUpdateFactory.zoomIn()); 
} 
+0

不幸的是,這並不總是返回一個加載的地圖,而只是可見和測量的視圖。目前我還沒有找到任何方法,但發佈了延時爲x毫秒延遲的Runnable。 – joecks

+0

第一:你想達到什麼目的?例如,如果您想縮放到特定的起始位置,則此finishedLoading()方法將正常工作。當用戶滾動時,加載磁貼在背景中始終是異步的,所以如果某個部分完全加載,回調會有多大意義?順便說一句:執行延遲是非常危險的,因爲要麼阻止你的用戶太長(沒有響應的應用程序),要麼你太短,數據將無法加載準備就緒。由於這取決於數據連接的速度,因此這是一個明確的No-Go! –

+0

有時'getViewTreeObserver'在偵聽器中沒有活動,即使在'addOnGlobalLayoutListener'之前活着。 –