0

Replicating some behavior in an iOS app to Android and one of the requirements is to be able to generate a map snapshot. Google maps does offers the ability to create a snapshot of a map but unlike the iOS app I don't see a way of creating the map purely in memory and then saving it to a DB. I've created maps before but it was always done with the intent of showing the map and the user interacting with it. In this scenario I don't want any of that. There should be no map to be viewed at this point. At a later time the generated image (bitmap) from the snapshot will be delivered to the user.

I have the onMapReady function all set like so

override fun onMapReady(map: GoogleMap?) {

    map?.let {
        //add route
        it.addPolyline(routeLines)

        //add markers
        for(marker in markerOptions) {
            it.addMarker(marker)
        }


        val callback = GoogleMap.SnapshotReadyCallback(){
            saveSnapshot(it)
        }
        it.snapshot(callback)
    }
}

But before the onMapReady can even be called, the map itself needs to initiated.

var mMap: GoogleMap? = null

How do I create a map in memory only with no visual?

Thanks ^.^

Micah Montoya
  • 757
  • 1
  • 8
  • 24

2 Answers2

0

Looks like this is a limitation of Google Maps. Reading the GoogleMaps class documentation, it couldn't be done like you can with MapKit in iOS.

Micah Montoya
  • 757
  • 1
  • 8
  • 24
0

You can use Maps Static API or try this solution based on MapView lite mode:

...
mMapView = new MapView(this, options);
mMapView.onCreate(mapViewBundle);

mMapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {

            // form your card (add markers, path etc.) here: 
            googleMap.addMarker(new MarkerOptions()
                    .position(KYIV)
                    .title("Kyiv"));

            // set map size in pixels and initiate image loading
            mMapView.measure(View.MeasureSpec.makeMeasureSpec(mMapWidth, View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(mMapHeight, View.MeasureSpec.EXACTLY));
            mMapView.layout(0, 0, mMapWidth, mMapHeight);

            googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {
                    mMapView.setDrawingCacheEnabled(true);
                    mMapView.measure(View.MeasureSpec.makeMeasureSpec(mMapWidth, View.MeasureSpec.EXACTLY),
                            View.MeasureSpec.makeMeasureSpec(mMapHeight, View.MeasureSpec.EXACTLY));
                    mMapView.layout(0, 0, mMapWidth, mMapHeight);
                    mMapView.buildDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(mMapView.getDrawingCache());
                    mMapView.setDrawingCacheEnabled(false);
                    mImageView.setImageBitmap(b);

                }
            });

        }
});
...
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79