Elye
Elye

Reputation: 60241

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

Upvotes: 8

Views: 9762

Answers (5)

Thiago
Thiago

Reputation: 13302

In Koltin its simpler:

                map.setOnMapLoadedCallback {
                   // your code here, sample update camera
                   map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding))
                }

Upvotes: 0

hans cruz
hans cruz

Reputation: 1

OnMapLoadedCallback is used for cycles such as custom photos in markers.

OnMapReadyCallback is for static markers.

Upvotes: -1

fweigl
fweigl

Reputation: 22038

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

Upvotes: 6

Alex_297
Alex_297

Reputation: 249

Between calls of OnMapReadyCallback and OnMapLoadedCallback there is a gap. Within this gap map is already not null but it's parts could be not finally initialized. In very rare situations you can get IllegalStateException after executing some methods after OnMapReadyCallback and before OnMapLoadedCallback. For example moving map camera can cause it by map.moveCamera or map.animateCamera.

But on the other hand waiting of OnMapLoadedCallback is protracting your map initialisation and user can see this. I've decided to use such approach

mapFragment.getMapAsync(new OnMapReadyCallback()
         {
            @Override
            public void onMapReady(GoogleMap googleMap) 
            {
                try {
                    setUpMap();
                }
                catch (IllegalStateException e)
                {
                    map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() 
                    {
                        @Override
                        public void onMapLoaded() {
                            setUpMap();
                        }
                    });
                }
            }
        });

Upvotes: 0

Pankaj
Pankaj

Reputation: 8058

As the Google Documentation says:

1. OnMapLoadedCallback :

Called when the map has finished rendering. This will only be called once. You must request another callback if you want to be notified again.

So, in this you have to check whether googleMap is null or not. If null then you have to initialise it. All the map tiles has been rendered and all the labeling also completed as defined in docs.

2. OnMapReadyCallback :

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.

So, in this you don't have to check for the null in onMapReady() method.

Upvotes: 2

Related Questions