Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleMap snapshot method returns white image

I'm trying to grab a screenshot of a Google Maps V2 map using the API mentioned here, however this seems to always return a white image with the Google logo at the bottom. If it wasn't for the logo I would have been sure this isn't working at all but the logo means that something is happening and a map just isn't showing. This is roughly what I'm doing:

mv.setVisibility(View.VISIBLE);
mv.getMap().snapshot(new GoogleMap.SnapshotReadyCallback() {
    public void onSnapshotReady(Bitmap snapshot) {
        synchronized(finished) {
            finished[0] = snapshot;
            finished.notify();
        }
    }
});

I tried multiple different approaches including drawing the image to a different image and various other attempts.

The only major difference is that I am using a MapView and not a MapFragment due to some technical issues I can't switch to using fragments here.

like image 474
Shai Almog Avatar asked Mar 16 '14 19:03

Shai Almog


People also ask

How do I take a screenshot on Google Maps in flutter?

for getting the screenshot of the only map content you can use GoogleMapController. takeSnapshot() function.

How do I take a screenshot of Google Maps on Android?

Press the Power and Volume down buttons at the same time. If that doesn't work, press and hold the Power button for a few seconds. Then tap Screenshot.


1 Answers

To take a snapshot you wait for the map to load then take a snapshot.

details: implement SnapshotReadyCallback and OnMapLoadedCallback.

import com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback;
import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;

public class KmlReader extends ActionBarActivity implements
     SnapshotReadyCallback,
    OnMapLoadedCallback {

...

 if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
// mMap = mMapFragment.getMap();
// // Check if we were successful in obtaining the map.

// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
        .findFragmentById(R.id.map)).getMap();
// use the settings maptype

// Check if we were successful in obtaining the map.
if (mMap != null) {

mMap.setOnMapLoadedCallback(this);

...

@Override
public void onMapLoaded() {
   if (mMap != null) {
        mMap.snapshot(this);
   }
}

You have a real bitmap when this fires.

@Override
public void onSnapshotReady(Bitmap bm) {
    if (CheckStorage.isExternalStorageWritable()) {
        int newHeight = 96;
        int newWidth = 96;

        int width = bm.getWidth();

        int height = bm.getHeight();

        float scaleWidth = ((float) newWidth) / width;

        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation

        Matrix matrix = new Matrix();

        // resize the bit map

        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap

        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
                matrix, false);
like image 72
danny117 Avatar answered Sep 17 '22 07:09

danny117