2013-01-19 92 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'之前活着。 –