Detect when Android v2 maps has loaded
Asked Answered
K

3

15

I'm writing a app that would take 9 snapshots of the map around an area when the user presses a button.

In loop, using this to move and save:

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mStartLat + (mMultiOffsetX + mWidth), mStartLng + (mMultiOffsetY + mHeight)), mZoom)); 
map.snapshot(this);

and in onSnapshotReady:

final String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + String.format("/test_maps/map_%f_%f.png", Double.valueOf(mStartLat + (mMultiOffsetX * mWidth)), Double.valueOf(mStartLng + (mMultiOffsetY * mHeight)));
FileOutputStream fos = new FileOutputStream(new File(fileName));
bmp.compress(CompressFormat.PNG, 0, fos);

Unfortunately the map occasionally finished loading when the snapshot is taken, so you end up with: Partially blurred map snapshot

So, is there any way to detect or set a listener for when the map view has finished loading?

Keitt answered 22/8, 2013 at 10:9 Comment(1)
A
50

OnMapLoadedCallback doesn't fire until after the tiles on map are loaded. Only fires once so you'll have to call it nine times to take nine snapshots.

When you have a reference to the map set the call back.

mMap.setOnMapLoadedCallback(this);

When the onMapLoaded event fires take the snapshot.

@Override
public void onMapLoaded() {
if (mMap != null) {
    mMap.snapshot(this);
}
}

See the documentation for further information: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback

Good Luck

**** history of waiting for this feature to be implemented.

Updates: Status: Fixed Labels: Fixed-Oct2013

Comment #3 on issue 5779 by [email protected]: Ability to be notified when the map is fully loaded/rendered http://code.google.com/p/gmaps-api-issues/issues/detail?id=5779

A map loaded callback interface has been added in the latest release of the Google Maps Android API v2.

See the documentation for further information: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback

Thanks to everyone for starring issue 5779 and 61675 Danny117

**** edit ****

Please star this issue if you come here. http://code.google.com/p/android/issues/detail?id=61675 Your star clicks will motivate google to move on this issue.

The map update 13 causes a problem with google play services so you can't use the new OnMapLoadedCallback yet. You can compile against it but the map api generates an error message in the logcat Google Play services out of date. Requires 4030500 but found 3266130

* edit * Brand new OnMapLoadedCallback

Updates: Status: Fixed Labels: Fixed-Oct2013

Comment #3 on issue 5779 by [email protected]: Ability to be notified when the map is fully loaded/rendered http://code.google.com/p/gmaps-api-issues/issues/detail?id=5779

A map loaded callback interface has been added in the latest release of the Google Maps Android API v2.

See the documentation for further information: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback

Thanks to everyone for starring issue 5779. Danny117

**** The following is deprecated *****

Looks like its a wait for update solution. I clicked the star for you as I was going to work on this feature in my own api v2 map.

edit*** This is the proposed new callback for when the map is rendered. Everyone that visits please follow link and star this issue. http://code.google.com/p/gmaps-api-issues/issues/detail?id=5779

*** previous snapshop callback will not be changed. Everyone that visits please follow link and star this issue. http://code.google.com/p/gmaps-api-issues/issues/detail?id=5712

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.SnapshotReadyCallback

Autrey answered 25/8, 2013 at 19:30 Comment(0)
W
4

As I saw this question haven't still been solved. Don't know why but for me when map have finished loading, the OnCameraChangeListener gets called. So I just use like this to detect when the map have finished loading.

googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition arg0) {
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    mStartLat + (mMultiOffsetX + mWidth), mStartLng
                            + (mMultiOffsetY + mHeight)), mZoom));
            map.snapshot(this);
        }
    });
Wahhabi answered 20/3, 2014 at 16:38 Comment(3)
This method is callend when you move the camera, not when the map is completely rendered.Pulchia
Deprecated method : @Deprecated public final void setOnCameraChangeListener(final GoogleMap.OnCameraChangeListener var1)Sidesman
Yes, it is deprecated. See developers.google.com/maps/documentation/android-sdk/events.Woodyard
S
0

Try using the ViewStub class to help you with lazy loading of your map. You can detect when the map is loaded using this code:

public void onShowMap(View v) {
    // where you put the MapView layout in ViewStub view
    ViewStub.setVisibility(View.VISIBLE);
}
Sladen answered 24/8, 2013 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.