我有一个谷歌地图,我告诉它适合它的相机到某些边界.也许不是很令人惊讶,我收到一个错误:
java.lang.IllegalStateException:地图大小不应为0.很可能,地图视图尚未出现布局.
所以让我们将这个问题抽象到任何View.
这个“布局”事件何时实际发生?例如,onMeasure()没有出现在Activity Lifecycle中.何时可以安全地调用我需要布局的方法?
解决方法
mapa.moveCamera(CameraUpdateFactory.newLatLngBounds(Builder.build(),this.getResources().getDisplayMetrics().widthPixels,this.getResources().getDisplayMetrics().heightPixels,50));
这是一项活动,相反
mapa.moveCamera(CameraUpdateFactory.newLatLngBounds(Builder.build(),50));
这对我有用.
文档
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.