From 671fe37b1e6cde56c3edec76ce62542e4c8e57b7 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Wed, 23 May 2018 10:52:22 +0200 Subject: Style JSON configuration on map snapshotter (#11976) * [core, android, darwin] - add style JSON configuration on MapSnapshotter --- .../mapboxsdk/snapshotter/MapSnapshotter.java | 14 ++++- .../src/main/AndroidManifest.xml | 11 ++++ .../snapshot/MapSnapshotterLocalStyleActivity.java | 71 ++++++++++++++++++++++ .../src/main/res/values/descriptions.xml | 1 + .../src/main/res/values/titles.xml | 1 + platform/android/scripts/exclude-activity-gen.json | 1 + .../android/src/snapshotter/map_snapshotter.cpp | 12 +++- .../android/src/snapshotter/map_snapshotter.hpp | 1 + platform/darwin/src/MGLMapSnapshotter.mm | 3 +- platform/default/mbgl/map/map_snapshotter.cpp | 15 +++-- platform/default/mbgl/map/map_snapshotter.hpp | 2 +- 11 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterLocalStyleActivity.java diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java index 0895096f6e..38d5277a80 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java @@ -86,6 +86,7 @@ public class MapSnapshotter { 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; @@ -111,6 +112,15 @@ public class MapSnapshotter { 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 @@ -208,7 +218,7 @@ public class MapSnapshotter { 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); } @@ -462,7 +472,7 @@ public class MapSnapshotter { 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); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index fb1d0ef8a2..b78fba0aae 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -344,6 +344,17 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".activity.FeatureOverviewActivity" /> + + + + 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()); + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml index 17d6ad57c6..b515a4d3ae 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml @@ -57,6 +57,7 @@ Show a static bitmap taken with the MapSnapshotter Show how to reuse a MapSnapshotter instance Show how to add a marker to a Snapshot + Show how to load a local style with a Snapshot Use Android SDK Animators to animate camera position changes Use Android SDK Views as symbols Use TextureView to render the map diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml index 3f011bd3ed..114ff38a0e 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml @@ -57,6 +57,7 @@ Map Snapshotter Map Snapshotter Reuse Map Snapshot with marker + Map Snapshot with local style Animator animation SymbolGenerator TextureView debug diff --git a/platform/android/scripts/exclude-activity-gen.json b/platform/android/scripts/exclude-activity-gen.json index 157808aa51..d7d9c16550 100644 --- a/platform/android/scripts/exclude-activity-gen.json +++ b/platform/android/scripts/exclude-activity-gen.json @@ -2,6 +2,7 @@ "BaseLocationActivity", "MapSnapshotterMarkerActivity", "MapSnapshotterReuseActivity", + "MapSnapshotterLocalStyleActivity", "LatLngBoundsActivity", "BottomSheetActivity", "MapSnapshotterActivity", diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index a006953d36..ca1307dd16 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -20,6 +20,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, jni::jint width, jni::jint height, jni::String styleURL, + jni::String styleJSON, jni::Object region, jni::Object position, jni::jboolean _showLogo, @@ -42,12 +43,19 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, if (region) { bounds = LatLngBounds::getLatLngBounds(_env, region); } + + std::pair style; + if (styleJSON) { + style = std::make_pair(true, jni::Make(_env, styleJSON)); + } else { + style = std::make_pair(false, jni::Make(_env, styleURL)); + } showLogo = _showLogo; // Create the core snapshotter snapshotter = std::make_unique(fileSource, *threadPool, - jni::Make(_env, styleURL), + style, size, pixelRatio, cameraOptions, @@ -141,7 +149,7 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) { // Register the peer jni::RegisterNativePeer(env, MapSnapshotter::javaClass, "nativePtr", - std::make_unique, jni::Object, jni::jfloat, jni::jint, jni::jint, jni::String, jni::Object, jni::Object, jni::jboolean, jni::String>, + std::make_unique, jni::Object, jni::jfloat, jni::jint, jni::jint, jni::String, jni::String, jni::Object, jni::Object, jni::jboolean, jni::String>, "nativeInitialize", "finalize", METHOD(&MapSnapshotter::setStyleUrl, "setStyleUrl"), diff --git a/platform/android/src/snapshotter/map_snapshotter.hpp b/platform/android/src/snapshotter/map_snapshotter.hpp index 4cdf4bcf2b..33d32e01a1 100644 --- a/platform/android/src/snapshotter/map_snapshotter.hpp +++ b/platform/android/src/snapshotter/map_snapshotter.hpp @@ -34,6 +34,7 @@ public: jni::jint width, jni::jint height, jni::String styleURL, + jni::String styleJSON, jni::Object region, jni::Object position, jni::jboolean showLogo, diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index 923c8fb2a1..76d99a0411 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -428,6 +428,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; _mbglThreadPool = mbgl::sharedThreadPool(); std::string styleURL = std::string([options.styleURL.absoluteString UTF8String]); + std::pair 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 @@ -454,7 +455,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; } // Create the snapshotter - _mbglMapSnapshotter = std::make_unique(*mbglFileSource, *_mbglThreadPool, styleURL, size, pixelRatio, cameraOptions, coordinateBounds); + _mbglMapSnapshotter = std::make_unique(*mbglFileSource, *_mbglThreadPool, style, size, pixelRatio, cameraOptions, coordinateBounds); } @end diff --git a/platform/default/mbgl/map/map_snapshotter.cpp b/platform/default/mbgl/map/map_snapshotter.cpp index 9341c23cfd..da3a250e0f 100644 --- a/platform/default/mbgl/map/map_snapshotter.cpp +++ b/platform/default/mbgl/map/map_snapshotter.cpp @@ -15,7 +15,7 @@ class MapSnapshotter::Impl { public: Impl(FileSource&, Scheduler&, - const std::string& styleURL, + const std::pair style, const Size&, const float pixelRatio, const CameraOptions&, @@ -43,7 +43,7 @@ private: MapSnapshotter::Impl::Impl(FileSource& fileSource, Scheduler& scheduler, - const std::string& styleURL, + const std::pair style, const Size& size, const float pixelRatio, const CameraOptions& cameraOptions, @@ -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 @@ -134,13 +137,13 @@ LatLngBounds MapSnapshotter::Impl::getRegion() const { MapSnapshotter::MapSnapshotter(FileSource& fileSource, Scheduler& scheduler, - const std::string& styleURL, + const std::pair style, const Size& size, const float pixelRatio, const CameraOptions& cameraOptions, const optional region, const optional programCacheDir) - : impl(std::make_unique>("Map Snapshotter", fileSource, scheduler, styleURL, size, pixelRatio, cameraOptions, region, programCacheDir)) { + : impl(std::make_unique>("Map Snapshotter", fileSource, scheduler, style, size, pixelRatio, cameraOptions, region, programCacheDir)) { } MapSnapshotter::~MapSnapshotter() = default; diff --git a/platform/default/mbgl/map/map_snapshotter.hpp b/platform/default/mbgl/map/map_snapshotter.hpp index 985396e5a3..a71da121c0 100644 --- a/platform/default/mbgl/map/map_snapshotter.hpp +++ b/platform/default/mbgl/map/map_snapshotter.hpp @@ -27,7 +27,7 @@ class MapSnapshotter { public: MapSnapshotter(FileSource& fileSource, Scheduler& scheduler, - const std::string& styleURL, + const std::pair style, const Size&, const float pixelRatio, const CameraOptions&, -- cgit v1.2.1