animateCamera可以工作,而moveCamera不适用于GoogleMap-Android [英] animateCamera works and moveCamera doesn't for GoogleMap - Android

查看:300
本文介绍了animateCamera可以工作,而moveCamera不适用于GoogleMap-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要移动相机以覆盖其上的所有标记.因此,我建立了LatLngBounds,然后尝试调用mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 15)).问题是当我使用moveCamera()方法时,我会得到IllegalStateException,但是当我使用animateCamera()时,它就可以了.我在onMapReady回调中调用了这两种方法.发生了什么事?

I need to move Camera to cover all Markers on it. So, I build LatLngBounds and then try to call mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 15)). Problem is when I use moveCamera() method, I'm getting IllegalStateException, but when I use animateCamera() it goes just fine. I call both methods in onMapReady callback. What is going on?

我的堆栈跟踪(主要部分):

My stacktrace (main part):

java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view.  Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.

一种方法如何知道地图大小而另一种方法不知道呢?

How is it possible that one method knows map size and the other one not?

推荐答案

请注意,OnMapReadyCallback不能保证地图已经过布局.因此,在调用回调方法时,可能尚未确定地图的大小.如果您需要了解尺寸或在API中调用需要了解尺寸的方法,请获取地图的View并注册一个ViewTreeObserver.OnGlobalLayoutListener.

Note that OnMapReadyCallback does not guarantee that the map has undergone layout. Therefore, the map's size may not have been determined by the time the callback method is called. If you need to know the dimensions or call a method in the API that needs to know the dimensions, get the map's View and register an ViewTreeObserver.OnGlobalLayoutListener as well.

请勿链接OnMapReadyCallback和OnGlobalLayoutListener侦听器,而应分别注册并等待两个回调,因为可以按任意顺序触发回调.

Do not chain the OnMapReadyCallback and OnGlobalLayoutListener listeners, but instead register and wait for both callbacks independently, since the callbacks can be fired in any order.

因此,您必须同时使用两个(onMapReady,onGlobalLayout)回调,以确保已完全加载地图并确定了大小.

So you have to use both (onMapReady,onGlobalLayout) callbacks to make sure that map have been fully loaded and size has been determined.

private GoogleMap mMap;
private boolean isMapLoaded;

SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager()
            .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mapFragment.getView().getViewTreeObserver().addOnGlobalLayoutListener(this);

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    if (!isMapLoaded) {
        isMapLoaded = true;
        return;
    }
    initMap();
}

@Override
public void onGlobalLayout() {
    if (!isMapLoaded) {
        isMapLoaded = true;
        return;
    }
    initMap();
}

private void initMap() {
   //maps fully loaded instance with defined size will be available here.
   //mMap.animateCamera();
   //mMap.moveCamera();
}

这篇关于animateCamera可以工作,而moveCamera不适用于GoogleMap-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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