Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moveCamera with CameraUpdateFactory.newLatLngBounds crashes

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?

like image 987
Lee Avatar asked Dec 03 '12 22:12

Lee


2 Answers

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:

map.setOnCameraChangeListener(new OnCameraChangeListener() {      @Override     public void onCameraChange(CameraPosition arg0) {         // Move camera.         map.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 10));         // Remove listener to prevent position reset on camera move.         map.setOnCameraChangeListener(null);     } }); 
like image 76
Paul Annekov Avatar answered Sep 20 '22 11:09

Paul Annekov


Those answers are just fine, but I would go for a different approach, a simpler one. If the method only works after the map is layed out, just wait for it:

map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {     @Override     public void onMapLoaded() {         map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 30));     } }); 
like image 40
Leandro Avatar answered Sep 18 '22 11:09

Leandro