// Copyright 2013 Google Inc. All Rights Reserved.
package com.example.mapdemo;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;
import com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
/**
* This shows how to take a snapshot of the map.
*/
public class SnapshotDemoActivity extends FragmentActivity {
/**
* Note that this may be null if the Google Play services APK is not available.
*/
private GoogleMap mMap;
private CheckBox mWaitForMapLoadCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.snapshot_demo);
mWaitForMapLoadCheckBox = (CheckBox) findViewById(R.id.wait_for_map_load);
createMapIfReady();
}
@Override
protected void onResume() {
super.onResume();
createMapIfReady();
}
private void createMapIfReady() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
}
}
/**
* Called when the snapshot button is clicked.
*/
public void onScreenshot(View view) {
takeSnapshot();
}
private void takeSnapshot() {
if (mMap == null) {
return;
}
final ImageView snapshotHolder = (ImageView) findViewById(R.id.snapshot_holder);
final SnapshotReadyCallback callback = new SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
// Callback is called from the main thread, so we can modify the ImageView safely.
snapshotHolder.setImageBitmap(snapshot);
}
};
if (mWaitForMapLoadCheckBox.isChecked()) {
mMap.setOnMapLoadedCallback(new OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
mMap.snapshot(callback);
}
});
} else {
mMap.snapshot(callback);
}
}
/**
* Called when the clear button is clicked.
*/
public void onClearScreenshot(View view) {
ImageView snapshotHolder = (ImageView) findViewById(R.id.snapshot_holder);
snapshotHolder.setImageDrawable(null);
}
}