gpt4 book ai didi

android - animateCamera 有效,而 moveCamera 不适用于 GoogleMap - Android

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:05 26 4
gpt4 key购买 nike

我需要移动相机以覆盖其上的所有标记。因此,我构建了 LatLngBounds,然后尝试调用 mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 15))。问题是,当我使用 moveCamera() 方法时,我得到了 IllegalStateException,但是当我使用 animateCamera() 时,一切正常。我在 onMapReady 回调中调用了这两种方法。这是怎么回事?

我的堆栈跟踪(主要部分):

java.lang.IllegalStateException: Error using newLatLngBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view.  Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions.

怎么可能一种方法知道 map 大小而另一种方法不知道呢?

最佳答案

根据 documentation , map 布局完成前不能使用该接口(interface)。它说

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.

但是您可以在 OnCameraChangeListener 中使用 newLatLngBounds() 方法。一切都会完美运行,您无需计算屏幕尺寸。据我所知,这个事件发生在 map 大小计算之后。

    mMap.setOnCameraChangeListener(new OnCameraChangeListener() {

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

关于android - animateCamera 有效,而 moveCamera 不适用于 GoogleMap - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com