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.
OnMapLoadedCallback : https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback?hl=en
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!!
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 .
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.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With