Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Map snapshotter additions #10163

Merged
merged 10 commits into from
Oct 31, 2017
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.mapbox.mapboxsdk.snapshotter;

import android.graphics.Bitmap;
import android.graphics.PointF;

import com.mapbox.mapboxsdk.geometry.LatLng;

/**
* A completed snapshot.
*
* @see MapSnapshotter
*/
public class MapSnapshot {

private long nativePtr = 0;
private Bitmap bitmap;
private String[] attributions;

/**
* Created from native side
*/
private MapSnapshot(long nativePtr, Bitmap bitmap, String[] attributions) {
this.nativePtr = nativePtr;
this.bitmap = bitmap;
this.attributions = attributions;
}

/**
* @return the bitmap
*/
public Bitmap getBitmap() {
return bitmap;
}

/**
* Calculate the point in pixels on the Image from geographical coordinates.
*
* @param latLng the geographical coordinates
* @return the point on the image
*/
public native PointF pixelForLatLng(LatLng latLng);

/**
* @return The attributions for the sources of this snapshot.
*/
protected String[] getAttributions() {
return attributions;
}

// Unused, needed for peer binding
private native void initialize();

protected native void finalize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.storage.FileSource;

/**
Expand All @@ -23,11 +22,27 @@
@UiThread
public class MapSnapshotter {

/**
* Get notified on snapshot completion.
*
* @see MapSnapshotter#start(SnapshotReadyCallback, ErrorHandler)
*/
public interface SnapshotReadyCallback {

/**
* Called when the snapshot is complete.
*
* @param snapshot the snapshot
*/
void onSnapshotReady(MapSnapshot snapshot);

}

/**
* Can be used to get notified of errors
* in snapshot generation
*
* @see MapSnapshotter#start(MapboxMap.SnapshotReadyCallback, ErrorHandler)
* @see MapSnapshotter#start(SnapshotReadyCallback, ErrorHandler)
*/
public interface ErrorHandler {

Expand All @@ -46,7 +61,7 @@ public interface ErrorHandler {
private long nativePtr = 0;

private final Context context;
private MapboxMap.SnapshotReadyCallback callback;
private SnapshotReadyCallback callback;
private ErrorHandler errorHandler;

/**
Expand Down Expand Up @@ -176,18 +191,18 @@ public MapSnapshotter(@NonNull Context context, @NonNull Options options) {
*
* @param callback the callback to use when the snapshot is ready
*/
public void start(@NonNull MapboxMap.SnapshotReadyCallback callback) {
public void start(@NonNull SnapshotReadyCallback callback) {
this.start(callback, null);
}

/**
* Starts loading and rendering the snapshot. The callbacks will be fired
* on the calling thread.
*
* @param callback the callback to use when the snapshot is ready
* @param callback the callback to use when the snapshot is ready
* @param errorHandler the error handler to use on snapshot errors
*/
public void start(@NonNull MapboxMap.SnapshotReadyCallback callback, ErrorHandler errorHandler) {
public void start(@NonNull SnapshotReadyCallback callback, ErrorHandler errorHandler) {
if (this.callback != null) {
throw new IllegalStateException("Snapshotter was already started");
}
Expand All @@ -197,12 +212,42 @@ public void start(@NonNull MapboxMap.SnapshotReadyCallback callback, ErrorHandle
nativeStart();
}

/**
* Updates the snapshotter with a new size
*
* @param width the width
* @param height the height
*/
public native void setSize(int width, int height);

/**
* Updates the snapshotter with a new {@link CameraPosition}
*
* @param cameraPosition the camera position
*/
public native void setCameraPosition(CameraPosition cameraPosition);

/**
* Updates the snapshotter with a new {@link LatLngBounds}
*
* @param region the region
*/
public native void setRegion(LatLngBounds region);

/**
* Updates the snapshotter with a new style url
*
* @param styleUrl the style url
*/
public native void setStyleUrl(String styleUrl);


/**
* Must be called in on the thread
* the object was created on.
*/
public void cancel() {
callback = null;
reset();
nativeCancel();
}

Expand All @@ -217,12 +262,12 @@ protected void addOverlay(Bitmap original) {
* Called by JNI peer when snapshot is ready.
* Always called on the origin (main) thread.
*
* @param bitmap the generated snapshot
* @param snapshot the generated snapshot
*/
protected void onSnapshotReady(Bitmap bitmap) {
protected void onSnapshotReady(MapSnapshot snapshot) {
if (callback != null) {
addOverlay(bitmap);
callback.onSnapshotReady(bitmap);
addOverlay(snapshot.getBitmap());
callback.onSnapshotReady(snapshot);
reset();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,26 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity android:name=".activity.snapshot.MapSnapshotterReuseActivity"
android:description="@string/description_map_snapshotter_reuse"
android:label="@string/activity_map_snapshotter_reuse">
<meta-data
android:name="@string/category"
android:value="@string/category_imagegenerator"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity android:name=".activity.snapshot.MapSnapshotterMarkerActivity"
android:description="@string/description_map_snapshotter_marker"
android:label="@string/activity_map_snapshotter_marker">
<meta-data
android:name="@string/category"
android:value="@string/category_imagegenerator"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.maplayout.DoubleMapActivity"
android:description="@string/description_doublemap"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.activity.snapshot;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewTreeObserver;
Expand All @@ -11,7 +10,7 @@
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.testapp.R;

Expand Down Expand Up @@ -96,12 +95,12 @@ private void startSnapShot(final int row, final int column) {

MapSnapshotter snapshotter = new MapSnapshotter(MapSnapshotterActivity.this, options);

snapshotter.start(new MapboxMap.SnapshotReadyCallback() {
snapshotter.start(new MapSnapshotter.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap snapshot) {
public void onSnapshotReady(MapSnapshot snapshot) {
Timber.i("Got the snapshot");
ImageView imageView = new ImageView(MapSnapshotterActivity.this);
imageView.setImageBitmap(snapshot);
imageView.setImageBitmap(snapshot.getBitmap());
grid.addView(
imageView,
new GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(column))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.mapbox.mapboxsdk.testapp.activity.snapshot;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.testapp.R;

import timber.log.Timber;

/**
* Test activity showing how to use a the {@link MapSnapshotter} and overlay
* {@link android.graphics.Bitmap}s on top.
*/
public class MapSnapshotterMarkerActivity extends AppCompatActivity implements MapSnapshotter.SnapshotReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_snapshotter_marker);

final View container = findViewById(R.id.container);
container.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//noinspection deprecation
container.getViewTreeObserver().removeGlobalOnLayoutListener(this);

Timber.i("Starting snapshot");

MapSnapshotter mapSnapshotter = new MapSnapshotter(
getApplicationContext(),
new MapSnapshotter
.Options(Math.min(container.getMeasuredWidth(), 1024), Math.min(container.getMeasuredHeight(), 1024))
.withStyle(Style.TRAFFIC_DAY)
.withCameraPosition(new CameraPosition.Builder().target(new LatLng(52.090737, 5.121420)).zoom(15).build())
);
mapSnapshotter.start(MapSnapshotterMarkerActivity.this);
}
});
}

@Override
public void onSnapshotReady(MapSnapshot snapshot) {
Timber.i("Snapshot ready");
ImageView imageView = (ImageView) findViewById(R.id.snapshot_image);
Bitmap image = addMarker(snapshot);
imageView.setImageBitmap(image);
}

private Bitmap addMarker(MapSnapshot snapshot) {
Canvas canvas = new Canvas(snapshot.getBitmap());
Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.mapbox_marker_icon_default, null);
// Dom toren
PointF markerLocation = snapshot.pixelForLatLng(new LatLng(52.090649433011315, 5.121310651302338));
canvas.drawBitmap(marker,
markerLocation.x,
/* Subtract height (in dp) so the bottom of the marker aligns correctly */
markerLocation.y - (marker.getHeight() / getResources().getDisplayMetrics().density),
null
);
return snapshot.getBitmap();
}

}
Loading