11

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!

user2682360
  • 157
  • 1
  • 1
  • 9
  • 1
    possible duplicate of [how to get snapshot from Google Map V2](http://stackoverflow.com/questions/16435842/how-to-get-snapshot-from-google-map-v2) – g00dy Aug 14 '13 at 12:14
  • 1
    check http://stackoverflow.com/a/18308611/2398886 with complete steps and example – Tarsem Singh Aug 19 '13 at 07:37

5 Answers5

16

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);
            }
        });
    }
antonio
  • 18,044
  • 4
  • 45
  • 61
David
  • 2,331
  • 4
  • 29
  • 42
10

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

justinkoh
  • 995
  • 10
  • 14
4

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.

Subhrajyoti Sen
  • 1,525
  • 14
  • 18
reza_khalafi
  • 6,230
  • 7
  • 56
  • 82
1

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 ;
        }
    }
Faisal Naseer
  • 4,110
  • 1
  • 37
  • 55
  • You can also try it without testing for initMap() which is a custom method defined to check if the map is initialized before taking snap. thanks – Faisal Naseer May 29 '15 at 08:18
0

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.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260