I found that the map.snapshot() method only works properly under special conditions. If called too early, it will crash, if called to late and to soon after setting camera it will return a snapshot of incorrect map image. I expect snapshot to return a snapshot of the map defined by the last given camera position and polylines. However it is difficult to get the GoogleMap-class to wait will it has fetched the correct tiles. It is easy if user clicks button to do snapshot because user knows when map is ready, its more difficult if it happens programmatically.
If you have the CameraPosition (LatLng, zoom) then it works well if MapFragment is created with GoogleMapOption:
GoogleMapOptions googleMapOptions = new GoogleMapOptions();
googleMapOptions.liteMode(true).mapType(GoogleMap.MAP_TYPE_NORMAL); // if this is what you want
googleMapOptions.camera(new CameraPosition(new LatLng(56, 10), 11, 0, 0));
MapFragment.newInstance(googleMapOptions);
If you don't have the CameraPosition yet, it is more difficult. You might not have the cameraposition yet if it is computed so that given markers are contained in camera, because that computation requires GoogleMap to be initialised with width/height. (chicken-and-egg problem, or do-it-yourself).
Subclass MapFragment and overwrite onCreate
public class FixedMapFragment extends MapFragment {
public void setOncreateListener(Listener listener) {
this.listener = listener;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
if (listener != null) {
listener.onCreate(v, mMapManager);
}
return v;
}
}
Then create your fragment, and set your onCreateListener. The onCreateListener should update cameraPosition and set polylines etc.
When onLoad on GoogleMap is invoked we should schedule yet another onLoad which does the snapshot (or we get wrong tiles again).
private void onLoad() {
// Set camera position and polylines here
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
snapshot(mSnapshotCallback);
}
});
}
Disadvantage: Sometimes map will flash an 'incorrect' world map on screen. To avoid this we can hide the map view until snapshat is taken using
mapView.setVisibility(VIEW.INVISIBLE);
This has been tested on google-maps 8.3.0 for android.