How to take google Maps v2 snapshot?

Try for Kotlin Android like this when click on button to take google map snapshot:

val snapshotReadyCallback : GoogleMap.SnapshotReadyCallback = GoogleMap.SnapshotReadyCallback { selectedScreenShot ->
    ivMapPreview.setImageBitmap(selectedScreenShot);
}

val onMapLoadedCallback : GoogleMap.OnMapLoadedCallback = GoogleMap.OnMapLoadedCallback {
    mMap!!.snapshot(snapshotReadyCallback)
}

mMap!!.setOnMapLoadedCallback(onMapLoadedCallback)

Be successful.


This one is better, it waits for your Map to be fully rendered, before taking the snapshot.

It was updated on 31-Oct-2013.

mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    public void onMapLoaded() {
        mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
            public void onSnapshotReady(Bitmap bitmap) {
                // Write image to disk
                FileOutputStream out = new FileOutputStream("/mnt/sdcard/map.png");
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            }
        });
    }
});

Extracted from http://googlegeodevelopers.blogspot.sg/2013/10/ghost-markers-in-your-neighborhood-new.html


You have to call the Google maps snapshot method in a button listener because if you should take it too early, it will give you error bitmap width has to be larger than 0 or something like this. Here is the code

private void button_listener() {
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SnapshotReadyCallback callback = new SnapshotReadyCallback() {
                    Bitmap bitmap;

                    @Override
                    public void onSnapshotReady(Bitmap snapshot) {
                        bitmap = snapshot;
                        try {
                            FileOutputStream out = new FileOutputStream("/mnt/sdcard/Download/TeleSensors.png");
                            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };

                map.snapshot(callback);
            }
        });
    }