Home » Android » android – How to take google Maps v2 snapshot?

android – How to take google Maps v2 snapshot?

Posted by: admin May 14, 2020 Leave a comment

Questions:

i have to solve this with the new “snapshot maker” which is implemented in the google maps release august but i dont’ know how to do this.
Can somone give me a simple example?

here is my code:

public class MainActivity extends Activity {
static LatLng HAMBURG = new LatLng(47.524749, 21.632745);
GoogleMap map;
File dbFile;
private File imageFile;


@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PolylineOptions line = new PolylineOptions();

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    /*
     * Adatbázis
     */
    try {
        dbFile = getDatabasePath("/mnt/sdcard/Download/TeleSensors.db");
    } catch (Exception e) {

    }

    SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(
            dbFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);

    Cursor curTAB = myDataBase.rawQuery("SELECT * FROM  GPS_Values;", null);

    Integer count = 0;
    while (curTAB.moveToNext()) {
        String s_latitude = curTAB.getString(1);
        String s_longitude = curTAB.getString(2);
        count++;
        double latitude = Double.parseDouble(s_latitude);
        double longitude = Double.parseDouble(s_longitude);
        line.add(new LatLng(latitude, longitude));

        Log.i("Coordinates",
                s_latitude.toString() + " --- " + s_longitude.toString());

    }
    curTAB.close();
    myDataBase.close();
    // adatbázis vége

    map.addPolyline(line);

    // map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    // map.setMapType(GoogleMap.MAP_TYPE_NONE);
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    // map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    // map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

    // Move the camera instantly to hamburg with a zoom of 15.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);  


}

}

Thank you very mouch!

How to&Answers:

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

Answer:

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

Answer:

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#snapshot(com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback, android.graphics.Bitmap)

public final void snapshot (GoogleMap.SnapshotReadyCallback callback)

Takes a snapshot of the map. You can use snapshots within your application when an interactive map would be difficult, or impossible,
to use. For example, images produced with the snapshot() method can be
used to display a thumbnail of the map in your app, or a snapshot in
the notification center.

Answer:

I’ ve tried the accepted answer but it didn’t work then tried another approach that worked for me.

private void CaptureScreen() {
        if(initMap()){
            SnapshotReadyCallback callback = new SnapshotReadyCallback() {
                Bitmap bitmap=null;

                @Override
                public void onSnapshotReady(Bitmap snapshot) {
                    // TODO Auto-generated method stub
                    bitmap = snapshot;
                    try {
                        saveImage(bitmap);
                        Toast.makeText(getApplicationContext(), "Image Saved", Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                private void saveImage(Bitmap bitmap) throws IOException{
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
                    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
                    f.createNewFile();
                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());
                    fo.close();

                }
            };

            mMap.snapshot(callback);
        }
        else{
            Toast.makeText(this, "Map is not Initialized yet", Toast.LENGTH_LONG).show();
            return ;
        }
    }