什么时候可以从OnMapReadyCallback调用onMapReady之后调用GoogleMap.moveCamera? [英] When can you call GoogleMap.moveCamera after onMapReady from OnMapReadyCallback?
问题描述
当前的Android Google Maps API要求您先使用OnMapReadyCallback
调用mapFragment.getMapAsync
,然后才能访问GoogleMap
.我以为一旦有了GoogleMap
,就可以安全地调用moveCamera()
了,但是我看到的IllegalStateException
崩溃报告显示为Map size can't be 0. Most likely, layout has not yet occured for the map view
.
The current Android Google Maps API requires you call mapFragment.getMapAsync
with a OnMapReadyCallback
before you can access the GoogleMap
. I assumed that once you had the GoogleMap
it would then be safe to call moveCamera()
but I was seeing crash reports with an IllegalStateException
which said Map size can't be 0. Most likely, layout has not yet occured for the map view
.
因此,我尝试添加ViewTreeObserver.OnPreDrawListener
,并将moveCamera()
调用移至onPreDraw
方法,因为该文档的内容为此时,树中的所有视图均已测量并给出了框架" .但是,我仍然看到一些崩溃报告,但都存在相同的问题.我找不到与此相关的任何文档-有类似问题 moveCamera与CameraUpdateFactory.newLatLngBounds崩溃,但它们早于getMapAsync
API的日期,因此并没有太大帮助.
So I tried adding a ViewTreeObserver.OnPreDrawListener
, and moving the moveCamera()
call to the onPreDraw
method, as the docs for that say "At this point, all views in the tree have been measured and given a frame". But I still see some crash reports with the same problem. I can't find any documentation for this — there's questions like moveCamera with CameraUpdateFactory.newLatLngBounds crashes but they pre-date the getMapAsync
API, so they're not much help.
推荐答案
我遇到了同样的问题.当应用尝试使用此相机更新来更改相机,直到该地图经过布局(此方法必须正确确定适当的边界框和缩放级别,该地图必须具有尺寸)时,此google地图库才会引发异常.它描述了
I had the same issue. This google maps library throws the exception when an app try to change the camera with this camera update until the map has undergone layout (in order for this method to correctly determine the appropriate bounding box and zoom level, the map must have a size). It described here.
这是我的解决方法:
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMapLoadedCallback(this);
}
@Override
public void onMapLoaded() {
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(…));
}
这篇关于什么时候可以从OnMapReadyCallback调用onMapReady之后调用GoogleMap.moveCamera?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!