Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnMapLoadedCallback and OnMapReadyCallback, when to use which?

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!!

like image 700
Elye Avatar asked Sep 04 '15 07:09

Elye


People also ask

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 .

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.


1 Answers

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).

like image 173
fweigl Avatar answered Nov 09 '22 09:11

fweigl