2

我想在谷歌地图加载后做点什么(地图块已经被填满)有没有办法做到这一点?

4

1 回答 1

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());
} 
于 2013-06-10T02:44:29.543 回答