Skip to content

Taking Snapshot of Maps

Abin K Paul edited this page Sep 8, 2021 · 2 revisions

Snapshots of maps can be taken using the MapSnapshot object.

The various flags available for configuring are:

  • INCLUDE_FLAG_UPTODATE : Accept only up-to-date tiles (Implicitly means that you may have to wait, and need background downloads)
  • INCLUDE_FLAG_EXPIRED : Accept expired tiles
  • INCLUDE_FLAG_SCALED : Accept scaled tiles
  • INCLUDE_FLAG_NOTFOUND : Accept empty tiles
  • INCLUDE_FLAGS_ALL : Include all above flags

Any combo of these flags can be used : Example : INCLUDE_FLAG_UPTODATE + INCLUDE_FLAG_SCALED is a valid flag

MapSnapshotable is a callback interface which gets called when the requested snapshot is ready

Sample 1 : Take a snapshot of a given MapView

final MapSnapshot mapSnapshot = new MapSnapshot(new MapSnapshot.MapSnapshotable() {
            @Override
            public void callback(final MapSnapshot pMapSnapshot) {
                if (pMapSnapshot.getStatus() != MapSnapshot.Status.CANVAS_OK) {
                    return;
                }
                final Bitmap bitmap = Bitmap.createBitmap(pMapSnapshot.getBitmap());
                // Do something with the bitmap like saving to file
            }
        }, MapSnapshot.INCLUDE_FLAG_UPTODATE, pMapView);
new Thread(mapSnapshot).start();

Sample 2: Taking snapshot given only latitude and longitude without having an explicit MapView

GeoPoint pt = new GeoPoint(latitude,longitude)

Marker marker = new Marker(new MapView(getApplicationContext()));
marker.setPosition(pt);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);

List<Overlay> mOverlay = new ArrayList<>();
mOverlay.add(marker);

MapTileProviderBase mapTileProviderBase = new MapTileProviderBasic(getApplicationContext());

// Create a new Projection (https://osmdroid.github.io/osmdroid/javadocAll/org/osmdroid/views/Projection.html)
Projection mProjection = new Projection(18.00, 400, 400, pt, 0, true, true, 0, 0); 

final MapSnapshot mapSnapshot = new MapSnapshot(new MapSnapshot.MapSnapshotable() {
                    @Override
                    public void callback(final MapSnapshot pMapSnapshot) {
                        // Save the current snapshot

                        if (pMapSnapshot.getStatus() != MapSnapshot.Status.CANVAS_OK) {
                            return;
                        }
                        final Bitmap bitmap = Bitmap.createBitmap(pMapSnapshot getBitmap());
                        // Do something with the bitmap like saving to file
                    }
                }, MapSnapshot.INCLUDE_FLAG_UPTODATE, mapTileProviderBase, mOverlay, mProjection);
new Thread(mapSnapshot).start();