From 5f808e0308e45d3c9b12d8a85a62e8503c62a940 Mon Sep 17 00:00:00 2001 From: liyanjin <36877356+grab-liyanjin@users.noreply.github.com> Date: Tue, 14 Oct 2025 21:32:27 +0800 Subject: [PATCH] Make Android snapshotter supports hiding the attribution (#3878) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .../src/cpp/snapshotter/map_snapshot.cpp | 8 ++++++-- .../src/cpp/snapshotter/map_snapshot.hpp | 1 + .../src/cpp/snapshotter/map_snapshotter.cpp | 5 ++++- .../src/cpp/snapshotter/map_snapshotter.hpp | 2 ++ .../maplibre/android/snapshotter/MapSnapshot.kt | 8 +++++++- .../android/snapshotter/MapSnapshotter.kt | 16 +++++++++++++++- 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.cpp b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.cpp index 8943ffc8c..10acbd950 100644 --- a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.cpp +++ b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.cpp @@ -31,6 +31,7 @@ jni::Local> MapSnapshot::New(JNIEnv& env, float pixelRatio, std::vector attributions, bool showLogo, + bool showAttribution, mbgl::MapSnapshotter::PointForFn pointForFn, mbgl::MapSnapshotter::LatLngForFn latLngForFn) { // Create the bitmap @@ -39,14 +40,17 @@ jni::Local> MapSnapshot::New(JNIEnv& env, // Create the Mapsnapshot peers static auto& javaClass = jni::Class::Singleton(env); static auto constructor = - javaClass.GetConstructor, jni::Array, jni::jboolean>(env); + javaClass + .GetConstructor, jni::Array, jni::jboolean, jni::jboolean>( + env); auto nativePeer = std::make_unique(pixelRatio, pointForFn, latLngForFn); return javaClass.New(env, constructor, reinterpret_cast(nativePeer.release()), bitmap, conversion::toArray(env, attributions), - (jni::jboolean)showLogo); + (jni::jboolean)showLogo, + (jni::jboolean)showAttribution); } void MapSnapshot::registerNative(jni::JNIEnv& env) { diff --git a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.hpp b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.hpp index d04a9aa50..5c74ed9d0 100644 --- a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.hpp +++ b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.hpp @@ -27,6 +27,7 @@ public: float pixelRatio, std::vector attributions, bool showLogo, + bool showAttribution, PointForFn pointForFn, LatLngForFn latLngForFn); diff --git a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp index 186b0cc3e..051fc359d 100644 --- a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp +++ b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp @@ -24,6 +24,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, const jni::Object& region, const jni::Object& position, jni::jboolean _showLogo, + jni::jboolean _showAttribution, const jni::String& _localIdeographFontFamily) : javaPeer(_env, _obj), pixelRatio(_pixelRatio) { @@ -39,6 +40,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, auto size = mbgl::Size{static_cast(width), static_cast(height)}; showLogo = _showLogo; + showAttribution = _showAttribution; // Create the core snapshotter snapshotter = std::make_unique( @@ -94,7 +96,7 @@ void MapSnapshotter::start(JNIEnv& env) { } else { // Create the wrapper auto mapSnapshot = android::MapSnapshot::New( - *_env, std::move(image), pixelRatio, attributions, showLogo, pointForFn, latLngForFn); + *_env, std::move(image), pixelRatio, attributions, showLogo, showAttribution, pointForFn, latLngForFn); // invoke callback static auto onSnapshotReady = javaClass.GetMethod)>(*_env, "onSnapshotReady"); @@ -327,6 +329,7 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) { const jni::Object&, const jni::Object&, jni::jboolean, + jni::jboolean, const jni::String&>, "nativeInitialize", "finalize", diff --git a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp index c2f69a076..af69dbeed 100644 --- a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp +++ b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp @@ -34,6 +34,7 @@ public: const jni::Object& region, const jni::Object& position, jni::jboolean showLogo, + jni::jboolean _showAttribution, const jni::String& localIdeographFontFamily); virtual ~MapSnapshotter() override; @@ -73,6 +74,7 @@ private: float pixelRatio; bool showLogo; + bool showAttribution; FileSource* jFileSource; void activateFilesource(JNIEnv&); diff --git a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshot.kt b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshot.kt index 37d48666b..0637040e7 100644 --- a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshot.kt +++ b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshot.kt @@ -11,7 +11,7 @@ import org.maplibre.android.geometry.LatLng * @see MapSnapshotter */ @Keep -class MapSnapshot private constructor(val nativePtr: Long, bitmap: Bitmap, attributions: Array, showLogo: Boolean) { +class MapSnapshot private constructor(val nativePtr: Long, bitmap: Bitmap, attributions: Array, showLogo: Boolean, showAttribution: Boolean) { /** * @return the large @@ -28,6 +28,11 @@ class MapSnapshot private constructor(val nativePtr: Long, bitmap: Bitmap, attri */ val isShowLogo: Boolean + /** + * @return Flag indicating to show the attribution. + */ + val isShowAttribution: Boolean + /** * Created from native side */ @@ -35,6 +40,7 @@ class MapSnapshot private constructor(val nativePtr: Long, bitmap: Bitmap, attri this.bitmap = bitmap this.attributions = attributions isShowLogo = showLogo + isShowAttribution = showAttribution } /** diff --git a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt index fcc836c97..c498b9402 100644 --- a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt +++ b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt @@ -134,6 +134,8 @@ open class MapSnapshotter(context: Context, options: Options) { var showLogo = true + var showAttribution = true + /** * @return the font family used for locally generating ideographs, * Default font for local ideograph font family is [MapLibreConstants.DEFAULT_FONT]. @@ -231,6 +233,15 @@ open class MapSnapshotter(context: Context, options: Options) { return this } + /** + * @param showAttribution The flag indicating to show the attribution. + * @return the mutated [Options] + */ + fun withAttribution(showAttribution: Boolean): Options { + this.showAttribution = showAttribution + return this + } + /** * Set the font family for generating glyphs locally for ideographs in the 'CJK Unified Ideographs' * and 'Hangul Syllables' ranges. @@ -337,6 +348,7 @@ open class MapSnapshotter(context: Context, options: Options) { options.region, options.cameraPosition, options.showLogo, + options.showAttribution, options.localIdeographFontFamily ) } @@ -476,7 +488,7 @@ open class MapSnapshotter(context: Context, options: Options) { */ protected open fun addOverlay(mapSnapshot: MapSnapshot) { val snapshot = mapSnapshot.bitmap - val canvas = Canvas(snapshot!!) + val canvas = Canvas(snapshot) val margin = context.resources.displayMetrics.density.toInt() * LOGO_MARGIN_DP drawOverlay(mapSnapshot, snapshot, canvas, margin) } @@ -511,6 +523,7 @@ open class MapSnapshotter(context: Context, options: Options) { private fun drawAttribution(mapSnapshot: MapSnapshot, canvas: Canvas, measure: AttributionMeasure, layout: AttributionLayout?) { // draw attribution + if (!mapSnapshot.isShowAttribution) return val anchorPoint = layout!!.anchorPoint if (anchorPoint != null) { drawAttribution(canvas, measure, anchorPoint) @@ -732,6 +745,7 @@ open class MapSnapshotter(context: Context, options: Options) { region: LatLngBounds?, position: CameraPosition?, showLogo: Boolean, + showAttribution: Boolean, localIdeographFontFamily: String? )