Is there a way to listen if a MapView
is still loading? Because I want to show a progress indicator (such as an hourglass) while the MapView
is downloading the map tiles.
Any solution?
You can use OnCameraChangeListener
and OnMapLoadedCallback
to simulate this behaviour.
From the documentation of GoogleMap.OnCameraChangeListener
:
public abstract void onCameraChange (CameraPosition position)
Called after the camera position has changed. During an animation, this listener may not be notified of intermediate camera positions. It is always called for the final position in the animation.
This is called on the main thread.
From the documentation of GoogleMap.OnMapLoadedCallback
:
public abstract void onMapLoaded ()
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.
As stated there, onMapLoaded()
will be only called once, so the strategy could be to request one GoogleMap.OnMapLoadedCallback
each time the onCameraChange
is called.
Here is a (very simple) example to illustrate the answer:
public class MapsActivity extends FragmentActivity implements OnCameraChangeListener, OnMapLoadedCallback {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = mapFragment.getMap();
googleMap.setOnCameraChangeListener(this);
}
@Override
public void onMapLoaded() {
//TODO: Hide your progress indicator
}
@Override
public void onCameraChange(final CameraPosition cameraPosition) {
googleMap.setOnMapLoadedCallback(this);
//TODO: Show your progress indicator
}
}
The onDraw documentation for MapView says
"We draw the map background (loading tiles as appropriate)"
You could try extending MapView and hiding progress indicator as onDraw finishes. But my guess would be that the MapTiles are loaded asynchronously so your progress bar would finish before the MapTiles were all loaded.
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