2

I have an app with a ViewSwitcher which holds a MapView and another View and I want to save the MapView's bitmap as an image. This works well, if the MapView has been visible at least once by doing something like that:

mapView.setDrawingCacheEnabled(true);
Bitmap bm = mapView.getDrawingCache();
/* ... save bitmap ... */

Problem is, if the MapView hasn't been visible, getDrawingCache() returns null for the bitmap. Is there any way how I can resolve that?

Ridcully
  • 23,362
  • 7
  • 71
  • 86

1 Answers1

1

I found out how to do it. There are two things to do:

To get the bitmap, create your own one and make the MapView draw into it:

    Bitmap bm = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    mapView.draw(canvas);

If you do so, you'll have all your overlays in the bitmap, but not the map image (the tiles). To achieve that too, you'll have to invoke mapView.preLoad() prior to drawing the map onto your bitmap.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • This is great, but I can't find a `preLoad()` method on my `MapView`? Weirdly, it's documented though. It's a `com.google.android.gms.maps.MapView` if that makes a difference. – Andrew Wyld Feb 26 '13 at 18:51
  • 2
    That makes a big difference as what you're using is Android Maps API V2 -- they've changed everything there and it is no longer possible to get an image of the map at all - because they are using a SurfaceView now to render it :-( – Ridcully Feb 26 '13 at 19:24
  • Ah, damn. Thank you, though! – Andrew Wyld Feb 27 '13 at 09:37