Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

android - How to take google Maps v2 snapshot?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...