Detect when Android v2 maps has loaded

假装没事ソ 提交于 2019-12-17 06:10:03

问题


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:

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


回答1:


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 schr...@google.com: 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 schr...@google.com: 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




回答2:


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);
        }
    });



回答3:


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);
}


来源:https://stackoverflow.com/questions/18377498/detect-when-android-v2-maps-has-loaded

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!