I'm making use of the new Android Google Maps API.
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.
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);
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)
If - instead of the newLatLngBounds()
factory method I use newLatLngZoom()
method then the same trap does not occur.
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?
Another approach would something like (Assuming your topmost view is a FrameLayout named
rootContainer
, even though it will work as long as you always choose your topmost container no matter which type or name it has):Modifying your camera functions to only work if
layoutDone
istrue
will solve all your problems without having to add extra functions or wire up logic to thelayoutListener
handler.OK I worked this out. As documented here that API can't be used pre-layout.
The correct API to use is described as:
To fix the problem I calculated my screen size and provided the width and height to
This then allowed me to specify the bounding box pre-layout.
You can use simple newLatLngBounds method in OnCameraChangeListener. All will be working perfectly and you don't need to calculate screen size. This event occurs after map size calculation (as I understand).
Example:
Ok I'm facing same issue. I have my fragment with my SupportmapFragment, ABS, and navigation drawer. What I did was:
And, by the way, I'm calling
resetCamera()
fromonCreateView
after inflating and before returning.What this does is catch the exception first time (while map "gets a size" as a way of saying it...) and then, other times I need to reset the camera, map already has size and does it through padding.
Issue is explained in documentation, it says:
I think it's a pretty decent solution. Hope it helps someone.
I used different approach which works for recent versions of Google Maps SDK (9.6+) and based on onCameraIdleListener. As I see so far it's callback method
onCameraIdle
called always afteronMapReady
. So my approach looks like this piece of code (considering it put inActivity
):Adding and removing markers can be done pre-layout completion, but moving the camera cannot (except using
newLatLngBounds(boundary, padding)
as noted in the OP's answer).Probably the best place to perform an initial camera update is using a one-shot
OnGlobalLayoutListener
as shown in Google's sample code, e.g. see the following excerpt fromsetUpMap()
in MarkerDemoActivity.java: