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

Style JSON configuration on map snapshotter #11976

Merged
merged 3 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static class Options {
private int width;
private int height;
private String styleUrl = Style.MAPBOX_STREETS;
private String styleJson;
private LatLngBounds region;
private CameraPosition cameraPosition;
private boolean showLogo = true;
Expand All @@ -111,6 +112,15 @@ public Options withStyle(String url) {
return this;
}

/**
* @param styleJson The style json to use
* @return the mutated {@link Options}
*/
public Options withStyleJson(String styleJson) {
this.styleJson = styleJson;
return this;
}

/**
* @param region the region to show in the snapshot.
* This is applied after the camera position
Expand Down Expand Up @@ -208,7 +218,7 @@ public MapSnapshotter(@NonNull Context context, @NonNull Options options) {
String programCacheDir = context.getCacheDir().getAbsolutePath();

nativeInitialize(this, fileSource, options.pixelRatio, options.width,
options.height, options.styleUrl, options.region, options.cameraPosition,
options.height, options.styleUrl, options.styleJson, options.region, options.cameraPosition,
options.showLogo, programCacheDir);
}

Expand Down Expand Up @@ -462,7 +472,7 @@ protected void reset() {

protected native void nativeInitialize(MapSnapshotter mapSnapshotter,
FileSource fileSource, float pixelRatio,
int width, int height, String styleUrl,
int width, int height, String styleUrl, String styleJson,
LatLngBounds region, CameraPosition position,
boolean showLogo, String programCacheDir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
</activity>
<activity
android:name=".activity.snapshot.MapSnapshotterLocalStyleActivity"
android:description="@string/description_map_snapshotter_local_style"
android:label="@string/activity_map_snapshotter_local_style">
<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
@@ -0,0 +1,71 @@
package com.mapbox.mapboxsdk.testapp.activity.snapshot;

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.geometry.LatLng;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
import timber.log.Timber;

import java.io.IOException;

/**
* Test activity showing how to use a the MapSnapshotter with a local style
*/
public class MapSnapshotterLocalStyleActivity extends AppCompatActivity
implements MapSnapshotter.SnapshotReadyCallback {

private MapSnapshotter mapSnapshotter;

@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);

String styleJson;
try {
styleJson = ResourceUtils.readRawResource(MapSnapshotterLocalStyleActivity.this, R.raw.sat_style);
} catch (IOException exception) {
throw new RuntimeException(exception);
}

Timber.i("Starting snapshot");
mapSnapshotter = new MapSnapshotter(
getApplicationContext(),
new MapSnapshotter
.Options(Math.min(container.getMeasuredWidth(), 1024), Math.min(container.getMeasuredHeight(), 1024))
.withStyleJson(styleJson)
.withCameraPosition(new CameraPosition.Builder().target(new LatLng(52.090737, 5.121420)).zoom(18).build())
);
mapSnapshotter.start(MapSnapshotterLocalStyleActivity.this, error -> Timber.e(error));
}
});
}

@Override
protected void onStop() {
super.onStop();
mapSnapshotter.cancel();
}

@Override
public void onSnapshotReady(MapSnapshot snapshot) {
Timber.i("Snapshot ready");
ImageView imageView = (ImageView) findViewById(R.id.snapshot_image);
imageView.setImageBitmap(snapshot.getBitmap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<string name="description_map_snapshotter">Show a static bitmap taken with the MapSnapshotter</string>
<string name="description_map_snapshotter_reuse">Show how to reuse a MapSnapshotter instance</string>
<string name="description_map_snapshotter_marker">Show how to add a marker to a Snapshot</string>
<string name="description_map_snapshotter_local_style">Show how to load a local style with a Snapshot</string>
<string name="description_camera_animator">Use Android SDK Animators to animate camera position changes</string>
<string name="description_symbol_generator">Use Android SDK Views as symbols</string>
<string name="description_textureview_debug">Use TextureView to render the map</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<string name="activity_map_snapshotter">Map Snapshotter</string>
<string name="activity_map_snapshotter_reuse">Map Snapshotter Reuse</string>
<string name="activity_map_snapshotter_marker">Map Snapshot with marker</string>
<string name="activity_map_snapshotter_local_style">Map Snapshot with local style</string>
<string name="activity_camera_animator">Animator animation</string>
<string name="activity_symbol_generator">SymbolGenerator</string>
<string name="activity_textureview_debug">TextureView debug</string>
Expand Down
1 change: 1 addition & 0 deletions platform/android/scripts/exclude-activity-gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"BaseLocationActivity",
"MapSnapshotterMarkerActivity",
"MapSnapshotterReuseActivity",
"MapSnapshotterLocalStyleActivity",
"LatLngBoundsActivity",
"BottomSheetActivity",
"MapSnapshotterActivity",
Expand Down
12 changes: 10 additions & 2 deletions platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
jni::jint width,
jni::jint height,
jni::String styleURL,
jni::String styleJSON,
jni::Object<LatLngBounds> region,
jni::Object<CameraPosition> position,
jni::jboolean _showLogo,
Expand All @@ -42,12 +43,19 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
if (region) {
bounds = LatLngBounds::getLatLngBounds(_env, region);
}

std::pair<bool, std::string> style;
if (styleJSON) {
style = std::make_pair(true, jni::Make<std::string>(_env, styleJSON));
} else {
style = std::make_pair(false, jni::Make<std::string>(_env, styleURL));
}

showLogo = _showLogo;
// Create the core snapshotter
snapshotter = std::make_unique<mbgl::MapSnapshotter>(fileSource,
*threadPool,
jni::Make<std::string>(_env, styleURL),
style,
size,
pixelRatio,
cameraOptions,
Expand Down Expand Up @@ -141,7 +149,7 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) {

// Register the peer
jni::RegisterNativePeer<MapSnapshotter>(env, MapSnapshotter::javaClass, "nativePtr",
std::make_unique<MapSnapshotter, JNIEnv&, jni::Object<MapSnapshotter>, jni::Object<FileSource>, jni::jfloat, jni::jint, jni::jint, jni::String, jni::Object<LatLngBounds>, jni::Object<CameraPosition>, jni::jboolean, jni::String>,
std::make_unique<MapSnapshotter, JNIEnv&, jni::Object<MapSnapshotter>, jni::Object<FileSource>, jni::jfloat, jni::jint, jni::jint, jni::String, jni::String, jni::Object<LatLngBounds>, jni::Object<CameraPosition>, jni::jboolean, jni::String>,
"nativeInitialize",
"finalize",
METHOD(&MapSnapshotter::setStyleUrl, "setStyleUrl"),
Expand Down
1 change: 1 addition & 0 deletions platform/android/src/snapshotter/map_snapshotter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MapSnapshotter {
jni::jint width,
jni::jint height,
jni::String styleURL,
jni::String styleJSON,
jni::Object<LatLngBounds> region,
jni::Object<CameraPosition> position,
jni::jboolean showLogo,
Expand Down
3 changes: 2 additions & 1 deletion platform/darwin/src/MGLMapSnapshotter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ - (void)setOptions:(MGLMapSnapshotOptions *)options
_mbglThreadPool = mbgl::sharedThreadPool();

std::string styleURL = std::string([options.styleURL.absoluteString UTF8String]);
std::pair<bool, std::string> style = std::make_pair(false, styleURL);

// Size; taking into account the minimum texture size for OpenGL ES
// For non retina screens the ratio is 1:1 MGLSnapshotterMinimumPixelSize
Expand All @@ -454,7 +455,7 @@ - (void)setOptions:(MGLMapSnapshotOptions *)options
}

// Create the snapshotter
_mbglMapSnapshotter = std::make_unique<mbgl::MapSnapshotter>(*mbglFileSource, *_mbglThreadPool, styleURL, size, pixelRatio, cameraOptions, coordinateBounds);
_mbglMapSnapshotter = std::make_unique<mbgl::MapSnapshotter>(*mbglFileSource, *_mbglThreadPool, style, size, pixelRatio, cameraOptions, coordinateBounds);
}

@end
15 changes: 9 additions & 6 deletions platform/default/mbgl/map/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MapSnapshotter::Impl {
public:
Impl(FileSource&,
Scheduler&,
const std::string& styleURL,
const std::pair<bool, std::string> style,
const Size&,
const float pixelRatio,
const CameraOptions&,
Expand Down Expand Up @@ -43,7 +43,7 @@ class MapSnapshotter::Impl {

MapSnapshotter::Impl::Impl(FileSource& fileSource,
Scheduler& scheduler,
const std::string& styleURL,
const std::pair<bool, std::string> style,
const Size& size,
const float pixelRatio,
const CameraOptions& cameraOptions,
Expand All @@ -52,8 +52,11 @@ MapSnapshotter::Impl::Impl(FileSource& fileSource,
: frontend(size, pixelRatio, fileSource, scheduler, programCacheDir)
, map(frontend, MapObserver::nullObserver(), size, pixelRatio, fileSource, scheduler, MapMode::Static) {

map.getStyle().loadURL(styleURL);

if (style.first) {
map.getStyle().loadJSON(style.second);
} else{
map.getStyle().loadURL(style.second);
}
map.jumpTo(cameraOptions);

// Set region, if specified
Expand Down Expand Up @@ -134,13 +137,13 @@ LatLngBounds MapSnapshotter::Impl::getRegion() const {

MapSnapshotter::MapSnapshotter(FileSource& fileSource,
Scheduler& scheduler,
const std::string& styleURL,
const std::pair<bool, std::string> style,
const Size& size,
const float pixelRatio,
const CameraOptions& cameraOptions,
const optional<LatLngBounds> region,
const optional<std::string> programCacheDir)
: impl(std::make_unique<util::Thread<MapSnapshotter::Impl>>("Map Snapshotter", fileSource, scheduler, styleURL, size, pixelRatio, cameraOptions, region, programCacheDir)) {
: impl(std::make_unique<util::Thread<MapSnapshotter::Impl>>("Map Snapshotter", fileSource, scheduler, style, size, pixelRatio, cameraOptions, region, programCacheDir)) {
}

MapSnapshotter::~MapSnapshotter() = default;
Expand Down
2 changes: 1 addition & 1 deletion platform/default/mbgl/map/map_snapshotter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MapSnapshotter {
public:
MapSnapshotter(FileSource& fileSource,
Scheduler& scheduler,
const std::string& styleURL,
const std::pair<bool, std::string> style,
const Size&,
const float pixelRatio,
const CameraOptions&,
Expand Down