8

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

Elye
  • 53,639
  • 54
  • 212
  • 474

5 Answers5

6

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

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • 1
    Thanks Ascorbin. it definitely good to use the one that is ready sooner. In what scenario would we use OnMapLoadedCallback then? – Elye Sep 04 '15 at 07:52
  • 1
    @Elye I don't know, perhaps one might show a ProgressBar until the map has fully loaded or hide the map and do some fancy fade in animation when the map is loaded. – fweigl Sep 04 '15 at 07:54
  • you can face issues if you only rely on onMapReady callback as stated here: https://stackoverflow.com/questions/13692579/movecamera-with-cameraupdatefactory-newlatlngbounds-crashes – Analizer Jan 30 '18 at 13:18
2

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.

Pankaj
  • 7,908
  • 6
  • 42
  • 65
  • Thanks @Clairvoyant. The "this will only be called once for `setOnMapLoadedCallback` is particularly true. One different I observed is, if I do quick scrolling on my recyclerview before the activity get loaded, the `setOnMapLoadedCallback` version might fail to move the camera of the the map, but `OnMapReadyCallback` works perfectly. I guess the quick scrolling before the activity load somehow have set MapAsLoaded before the camera move. So it didn't get to move on eventually. In short, my recommendation now is to use OnMapReadyCallback if the map is used as a ViewHolder in RecyclerView. – Elye Sep 04 '15 at 11:10
  • 1
    On my opinion **OnMapReadyCallback** is better also because we don't have to check **null** condition for the google map. – Pankaj Sep 04 '15 at 11:36
0

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();
                        }
                    });
                }
            }
        });
Alex_297
  • 249
  • 1
  • 8
  • I don`t get the IllegalStateException, all I see is com.google.maps.api.android.lib6.common.apiexception. Maybe they changed it. – Analizer Jan 30 '18 at 13:20
0

In Koltin its simpler:

                map.setOnMapLoadedCallback {
                   // your code here, sample update camera
                   map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding))
                }
Thiago
  • 12,778
  • 14
  • 93
  • 110
-1

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

OnMapReadyCallback is for static markers.

hans cruz
  • 1
  • 1
  • 1
    Hans, please write your answer in English, as Stack Overflow is an [English only site](https://meta.stackoverflow.com/a/297680). – 4b0 Aug 08 '18 at 03:45