OnMapLoadedCallback and OnMapReadyCallback, when to use which?

Elye picture Elye · Sep 4, 2015 · Viewed 7.4k times · Source

While working a ViewHolder of GoogleMap Lite, as part of the row in RecyclerView, I'm looking for callback to set the pins location when the Map is ready. I found both function below.

  1. OnMapLoadedCallback : https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback?hl=en

  2. OnMapReadyCallback : https://developers.google.com/android/reference/com/google/android/gms/maps/OnMapReadyCallback

Both also proven working and usable (as shown below). Hence I'm puzzled if they do have any specific different behaviour that should be used at different occasion, or they are indeed similar and could be used interchangably?

The use of OnMapLoadedCallback:

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (Marker marker : markers) {
        builder.include(marker.getPosition());
    }

    final CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(builder.build(), 0);
    googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
        @Override
        public void onMapLoaded() {
            googleMap.moveCamera(cameraUpdate);
        }
    });

The use of OnMapReadyCallback:

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (Marker marker : markers) {
        builder.include(marker.getPosition());
    }

    final CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(builder.build(), 0);
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            googleMap.moveCamera(cameraUpdate);
        }
    });

Thanks!!

Answer

fweigl picture fweigl · Sep 4, 2015

You can safely use OnMapReadyCallback to set your pins. It is called as soon as the map is ready for you to use it.

OnMapLoadedCallback, as the docs state, is called

when the map has finished rendering. This occurs after all tiles required to render the map have been fetched, and all labeling is complete.

eg. the map's content is fully loaded and visible.

This happens later than OnMapReady. I don't see a reason to wait for that event.

EDIT: The call googleMap.setOnMapLoadedCallback even implies that OnMapReady already happened to be able to be called safely (googleMap != null).