Android googlemaps v2完成加载事件或回调 [英] Android googlemaps v2 finish loading event or callback

查看:178
本文介绍了Android googlemaps v2完成加载事件或回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

我想在谷歌地图加载(maptiles已经填写)后做某事p>如 qubz 所述,ViewTreeObserver可以用于在地图加载完成后实现回调,所以用户将获得eg启动后正确的位置:

  @Override 
public void onCreate(Bundle savedInstanceState){
//这是为用户启用onMapLoadingCompleted功能的小黑客。
final查看mapView = getSupportFragmentManager()。findFragmentById(R.id.google_map_fragment).getView();
if(mapView.getViewTreeObserver()。isAlive()){
mapView.getViewTreeObserver()。addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@SuppressWarnings(deprecation)
/ /我们在支持时使用新的方法
@SuppressLint(NewApi)
//我们检查我们正在使用哪个版本
@Override
public void onGlobalLayout(){
if(Build.VERSION.SDK_INT< Build.VERSION_CODES.JELLY_BEAN){
mapView.getViewTreeObserver()。removeGlobalOnLayoutListener(this);
} else {
mapView.getViewTreeObserver ).removeOnGlobalLayoutListener(this);
}
//发送地图加载完成的通知
onMapFinishedLoading();
}
});
}
}

protected void onMapFinishedLoading(){
//做任何你想要做的事情。此时地图已完成载入。
Log.i(TAG,映射完成加载);
GoogleMap mMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.google_map_fragment))
.getMap();
mMap.moveCamera(CameraUpdateFactory.zoomIn());
}


I want to do something after the google maps has loaded(maptiles have been filled) is there anyway to achieve that?

解决方案

As noted by qubz, the ViewTreeObserver can be used to achieve a callback after the loading of the map has completed, so the user will get e.g. the correct location right after start-up:

@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());
} 

这篇关于Android googlemaps v2完成加载事件或回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆