Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Google MapView show a progress indicator while map is loading

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?

like image 276
lory105 Avatar asked Aug 30 '12 08:08

lory105


2 Answers

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
    }
}
like image 118
antonio Avatar answered Nov 02 '22 17:11

antonio


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.

like image 34
Paul D'Ambra Avatar answered Nov 02 '22 17:11

Paul D'Ambra