首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有CameraUpdateFactory.newLatLngBounds的moveCamera崩溃

带有CameraUpdateFactory.newLatLngBounds的moveCamera崩溃
EN

Stack Overflow用户
提问于 2012-12-04 06:04:23
回答 14查看 83.2K关注 0票数 96

我正在使用新的Android Google Maps API

我创建了一个包含MapFragment的活动。在活动onResume中,我将标记设置到GoogleMap对象中,然后为包含所有标记的地图定义一个边界框。

这使用了以下伪代码:

代码语言:javascript
复制
LatLngBounds.Builder builder = new LatLngBounds.Builder();
while(data) {
   LatLng latlng = getPosition();
   builder.include(latlng);
}
CameraUpdate cameraUpdate = CameraUpdateFactory
   .newLatLngBounds(builder.build(), 10);
map.moveCamera(cameraUpdate);

map.moveCamera()的调用会导致我的应用程序崩溃,堆栈如下:

代码语言:javascript
复制
Caused by: java.lang.IllegalStateException: 
    Map size should not be 0. Most likely, layout has not yet 

    at maps.am.r.b(Unknown Source)
    at maps.y.q.a(Unknown Source)
    at maps.y.au.a(Unknown Source)
    at maps.y.ae.moveCamera(Unknown Source)
    at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub
        .onTransact(IGoogleMapDelegate.java:83)
    at android.os.Binder.transact(Binder.java:310)
    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a
        .moveCamera(Unknown Source)
    at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
    at ShowMapActivity.drawMapMarkers(ShowMapActivity.java:91)
    at ShowMapActivity.onResume(ShowMapActivity.java:58)
    at android.app.Instrumentation
        .callActivityOnResume(Instrumentation.java:1185)
    at android.app.Activity.performResume(Activity.java:5182)
    at android.app.ActivityThread
        .performResumeActivity(ActivityThread.java:2732)

如果我使用newLatLngZoom()方法而不是newLatLngBounds()工厂方法,那么同样的陷阱就不会发生。

在GoogleMap对象上绘制标记的最佳位置是在onResume上吗?还是应该在其他地方绘制标记并设置相机位置?

EN

回答 14

Stack Overflow用户

发布于 2014-03-20 08:27:58

这些答案都很好,但我会选择一种不同的方法,一种更简单的方法。如果该方法仅在地图布局后才起作用,请等待它:

代码语言:javascript
复制
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 30));
    }
});
票数 57
EN

Stack Overflow用户

发布于 2013-01-28 17:52:00

解决方案要简单得多……

代码语言:javascript
复制
// Pan to see all markers in view.
try {
    this.gmap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
} catch (IllegalStateException e) {
    // layout not yet initialized
    final View mapView = getFragmentManager()
       .findFragmentById(R.id.map).getView();
    if (mapView.getViewTreeObserver().isAlive()) {
        mapView.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @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);
                }
                gmap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
            }
        });
    }
}

捕获IllegalStateException并在视图上使用全局侦听器。使用其他方法预先设置边界大小(预先布局)意味着您必须计算视图的大小,而不是屏幕设备的大小。只有在地图全屏显示且不使用碎片的情况下,它们才会匹配。

票数 34
EN

Stack Overflow用户

发布于 2014-12-30 17:50:11

这是我的解决方案,在这种情况下,只需等待地图加载完成。

代码语言:javascript
复制
final int padding = getResources().getDimensionPixelSize(R.dimen.spacing);    

try {
    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(pCameraBounds, padding));
} catch (IllegalStateException ise) {

    mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {

        @Override
        public void onMapLoaded() {
            mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(pCameraBounds, padding));
        }
    });
}
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13692579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档