【问题标题】:Why does GoogleMap.animateCamera() crash after onGlobalLayout()?为什么 GoogleMap.animateCamera() 在 onGlobalLayout() 之后会崩溃?
【发布时间】:2013-01-22 10:28:08
【问题描述】:

我对嵌入新 Google Maps API 中的 SuportMapFragment 的 Fragment 有疑问。当我的片段被创建时,它会在AsyncTask 中获取一些数据,从onResume 方法开始。发生这种情况时,MapFragment 保持在屏幕外,而是显示一个进度条。

AsyncTask完成后,我注册了mapView的onGlobalLayout事件。然后我显示片段,这导致地图视图被显示和布局。 onGlobalLayout 被触发,动画开始。

这在相当长的一段时间内运行良好,直到我开始优化数据收集AsyncTask 中的操作以几乎立即完成。现在应用程序在启动时经常会崩溃,在 Release 中比在 Debug 中更常见,这就是为什么我认为我正在处理我不知道的赛车情况。

异常如下:

地图大小不应为 0。很可能,地图视图的布局尚未发生。

非常感谢任何见解。

供参考:这是似乎导致崩溃的代码:

// Hide status UI
this.progressBar.setVisibility(View.INVISIBLE);
this.statusTextView.setVisibility(View.INVISIBLE);

// Update state
this.uraDataDownloadFinished = true;

// Schedule map translation to occur when layout is complete
final View mapView = this.mapFragment.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()
        {
            Log.d(TAG, "onGlobalLayout()");
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
            {
              mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
            else
            {
              mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }

            // here we crash
            this.map.animateCamera(
                    CameraUpdateFactory.newLatLngBounds(
                            LatLngBounds.builder()
                                .include(topLeft)
                                .include(topRight)
                                .include(bottomLeft)
                                .include(bottomRight)
                                .build(),
                            0),
                    durationMs,
                    null);
        }
   });
}

// Show map (which will eventually trigger onGlobalLayout)
this.getFragmentManager().beginTransaction().show(this.mapFragment).commit();
this.mapFragment.getMap().setOnCameraChangeListener(this);

非常感谢!

【问题讨论】:

    标签: android google-maps-android-api-2


    【解决方案1】:

    我不确定,但是当您使用 AsyncTask 时,您可能正在尝试在地图进行布局之前获取地图边界。

    可以看到documentation,建议改用newLatLngBounds(boundary, width, height, padding)

    【讨论】:

    • 谢谢,这行得通。由于地图完全填满了布局容器,因此我可以改用它的尺寸。但是,我的问题仍然存在。既然在等onGlobalLayout(),应该不会遇到这个问题吧?
    • 不,我不这么认为,你必须等到地图完成布局。
    • 我明白了,所以MapView 的布局不代表地图布局。只是出于好奇:有没有办法听地图的布局过程,还是不可能?我没有看到任何明显的 API。
    • 我从不使用它,但你可以按照这个post
    【解决方案2】:

    试试这个,它对我有用:

    map.setOnCameraChangeListener(new OnCameraChangeListener() {
    
        @Override
        public void onCameraChange(CameraPosition arg0) {
            // Move camera.
            map.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 10));
            // Remove listener to prevent position reset on camera move.
            map.setOnCameraChangeListener(null);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多