带有 CameraUpdateFactory.newLatLngBounds 的 moveCamera 崩溃 [英] moveCamera with CameraUpdateFactory.newLatLngBounds crashes

查看:30
本文介绍了带有 CameraUpdateFactory.newLatLngBounds 的 moveCamera 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的 AndroidGoogle 地图 API.

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

I create an activity which includes a MapFragment. In the activity onResume I set the markers into the GoogleMap object and then define a bounding box for the map which includes all of the markers.

这是使用以下伪代码:

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() 的调用导致我的应用程序因以下堆栈崩溃:

The call to map.moveCamera() causes my application to crash with the following stack:

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)

如果 - 而不是 newLatLngBounds() 工厂方法,我使用 newLatLngZoom() 方法,则不会发生相同的陷阱.

If - instead of the newLatLngBounds() factory method I use newLatLngZoom() method then the same trap does not occur.

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

Is the onResume the best place to draw the markers onto the GoogleMap object or should I be drawing the markers and setting the camera position somewhere else?

推荐答案

好的,我解决了这个问题.正如此处所述,该 API 不能在布局前使用.

OK I worked this out. As documented here that API can't be used pre-layout.

要使用的正确 API 描述为:

The correct API to use is described as:

注意:只使用更简单的方法 newLatLngBounds(boundary, padding)如果要使用 CameraUpdate 来移动地图经过布局后的相机.在布局期间,API计算需要的地图的显示边界正确投影边界框.相比之下,您可以使用由更复杂的方法返回的 CameraUpdatenewLatLngBounds(boundary, width, height, padding) 随时,甚至在地图经过布局之前,因为 API 计算显示您传递的参数的边界.

Note: Only use the simpler method newLatLngBounds(boundary, padding) to generate a CameraUpdate if it is going to be used to move the camera after the map has undergone layout. During layout, the API calculates the display boundaries of the map which are needed to correctly project the bounding box. In comparison, you can use the CameraUpdate returned by the more complex method newLatLngBounds(boundary, width, height, padding) at any time, even before the map has undergone layout, because the API calculates the display boundaries from the arguments that you pass.

为了解决这个问题,我计算了我的屏幕尺寸并将宽度和高度提供给

To fix the problem I calculated my screen size and provided the width and height to

public static CameraUpdate newLatLngBounds(
    LatLngBounds bounds, int width, int height, int padding)

这允许我指定边界框预布局.

This then allowed me to specify the bounding box pre-layout.

这篇关于带有 CameraUpdateFactory.newLatLngBounds 的 moveCamera 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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