Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting until the google map has size in a MapFragment

My activity contains a MapFragment in a LinearLayout. I do the following

in onCreate:

  • I inflate this layout using setContentView in the onCreate method of my activity.
  • Get a handle to the GoogleMap using getMap().

in onStart:

  • I get some place coordinates from an SQLite Database
  • add corresponding markers to the map
  • add these points to a LatLngBounds.Builder
  • animate the camera using newLatLngBounds(Builder.build(), 10)

According to maps api reference, I shouldn't call newLatLngBounds(LatLngBounds bounds, int padding) before making sure that the map has a size. I indeed get an IllegalStateException at this point. But what is the right method for waiting until the map has a size?

like image 475
ercan Avatar asked Jan 27 '14 14:01

ercan


People also ask

What is MapFragment?

public class MapFragment extends Fragment. A Map component in an app. This fragment is the simplest way to place a map in an application. It's a wrapper around a view of a map to automatically handle the necessary life cycle needs.

What is OnMapReadyCallback?

public interface OnMapReadyCallback. Callback interface for when the map is ready to be used. Once an instance of this interface is set on a MapFragment or MapView object, the onMapReady(GoogleMap) method is triggered when the map is ready to be used and provides a non-null instance of GoogleMap .

Where do I get GoogleMap API key?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key.


2 Answers

The solution by rusmus didn't work for me. I used this one instead:

    map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
        @Override
        public void onMapLoaded() {
            map.animateCamera(cameraUpdate);
        }
    });

If you know the map size, you can avoid waiting and move the camera before the map is displayed. We finally used the display size as an approximation of the map size (but you could find out the exact size if you want to be more precise):

    final DisplayMetrics display = getResources().getDisplayMetrics();
    final int padding = display.widthPixels / 20;
    final CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(
        boundsBuilder.build(), display.widthPixels, display.heightPixels, padding);
    map.moveCamera(cameraUpdate);
like image 61
Ferran Maylinch Avatar answered Sep 27 '22 17:09

Ferran Maylinch


I have succesfully used the following code in the past:

final LatLngBounds.Builder builder = new LatLngBounds.Builder();
final View mapView = fragment.getView();
final GoogleMap map = fragment.getMap():

//Add points to builder. And get bounds...
final LatLngBounds bounds = builder.build();

// Pan to see all markers in view.
// Cannot zoom to bounds until the map has a size.

if (mapView.getViewTreeObserver().isAlive()) {
    mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        public void onGlobalLayout() {
            mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50), 1500, null);         
        }
    });
}
like image 30
rusmus Avatar answered Sep 27 '22 16:09

rusmus