Android googlemaps v2完成加载事件或回调

时间:2013-01-19 22:01:36

标签: android events callback loading google-maps-android-api-2

我想在加载谷歌地图之后做一些事情(maptiles已被填充)无论如何都要实现这一目标吗?

1 个答案:

答案 0 :(得分: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());
}