moveCamera与CameraUpdateFactory.newLatLngBounds崩溃 [英] moveCamera with CameraUpdateFactory.newLatLngBounds crashes

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

问题描述

我正在使用新的<一个href="https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/package-summary">Android谷歌地图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.

这是使用下面的伪code:

This is using the following pseudo code:

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)

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

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?

推荐答案

确定我工作了这一点。作为<一个href="https://developers.google.com/maps/documentation/android/views#changing_camera_position">documented此处该API不能用于$ P $对布局

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(边界,填充)   产生,如果它要被用于移动一个CameraUpdate   地图后,相机发生了布局。在布局过程中,该API   计算的地图,这是需要的显示的边界   正确投影边框。相比较而言,可以使用   CameraUpdate通过更复杂的方法返回   newLatLngBounds(边界,宽度,高度,填充)在任何时间,甚至   之前在地图上已经发生了布局,因为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)

这则让我指定边框pre-布局。

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

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

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