Skip to content

Commit 5f808e0

Browse files
Make Android snapshotter supports hiding the attribution (maplibre#3878)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6a66343 commit 5f808e0

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed

platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jni::Local<jni::Object<MapSnapshot>> MapSnapshot::New(JNIEnv& env,
3131
float pixelRatio,
3232
std::vector<std::string> attributions,
3333
bool showLogo,
34+
bool showAttribution,
3435
mbgl::MapSnapshotter::PointForFn pointForFn,
3536
mbgl::MapSnapshotter::LatLngForFn latLngForFn) {
3637
// Create the bitmap
@@ -39,14 +40,17 @@ jni::Local<jni::Object<MapSnapshot>> MapSnapshot::New(JNIEnv& env,
3940
// Create the Mapsnapshot peers
4041
static auto& javaClass = jni::Class<MapSnapshot>::Singleton(env);
4142
static auto constructor =
42-
javaClass.GetConstructor<jni::jlong, jni::Object<Bitmap>, jni::Array<jni::String>, jni::jboolean>(env);
43+
javaClass
44+
.GetConstructor<jni::jlong, jni::Object<Bitmap>, jni::Array<jni::String>, jni::jboolean, jni::jboolean>(
45+
env);
4346
auto nativePeer = std::make_unique<MapSnapshot>(pixelRatio, pointForFn, latLngForFn);
4447
return javaClass.New(env,
4548
constructor,
4649
reinterpret_cast<jlong>(nativePeer.release()),
4750
bitmap,
4851
conversion::toArray(env, attributions),
49-
(jni::jboolean)showLogo);
52+
(jni::jboolean)showLogo,
53+
(jni::jboolean)showAttribution);
5054
}
5155

5256
void MapSnapshot::registerNative(jni::JNIEnv& env) {

platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshot.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class MapSnapshot {
2727
float pixelRatio,
2828
std::vector<std::string> attributions,
2929
bool showLogo,
30+
bool showAttribution,
3031
PointForFn pointForFn,
3132
LatLngForFn latLngForFn);
3233

platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
2424
const jni::Object<LatLngBounds>& region,
2525
const jni::Object<CameraPosition>& position,
2626
jni::jboolean _showLogo,
27+
jni::jboolean _showAttribution,
2728
const jni::String& _localIdeographFontFamily)
2829
: javaPeer(_env, _obj),
2930
pixelRatio(_pixelRatio) {
@@ -39,6 +40,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
3940
auto size = mbgl::Size{static_cast<uint32_t>(width), static_cast<uint32_t>(height)};
4041

4142
showLogo = _showLogo;
43+
showAttribution = _showAttribution;
4244

4345
// Create the core snapshotter
4446
snapshotter = std::make_unique<mbgl::MapSnapshotter>(
@@ -94,7 +96,7 @@ void MapSnapshotter::start(JNIEnv& env) {
9496
} else {
9597
// Create the wrapper
9698
auto mapSnapshot = android::MapSnapshot::New(
97-
*_env, std::move(image), pixelRatio, attributions, showLogo, pointForFn, latLngForFn);
99+
*_env, std::move(image), pixelRatio, attributions, showLogo, showAttribution, pointForFn, latLngForFn);
98100

99101
// invoke callback
100102
static auto onSnapshotReady = javaClass.GetMethod<void(jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady");
@@ -327,6 +329,7 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) {
327329
const jni::Object<LatLngBounds>&,
328330
const jni::Object<CameraPosition>&,
329331
jni::jboolean,
332+
jni::jboolean,
330333
const jni::String&>,
331334
"nativeInitialize",
332335
"finalize",

platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class MapSnapshotter final : public mbgl::MapSnapshotterObserver {
3434
const jni::Object<LatLngBounds>& region,
3535
const jni::Object<CameraPosition>& position,
3636
jni::jboolean showLogo,
37+
jni::jboolean _showAttribution,
3738
const jni::String& localIdeographFontFamily);
3839

3940
virtual ~MapSnapshotter() override;
@@ -73,6 +74,7 @@ class MapSnapshotter final : public mbgl::MapSnapshotterObserver {
7374

7475
float pixelRatio;
7576
bool showLogo;
77+
bool showAttribution;
7678

7779
FileSource* jFileSource;
7880
void activateFilesource(JNIEnv&);

platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshot.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import org.maplibre.android.geometry.LatLng
1111
* @see MapSnapshotter
1212
*/
1313
@Keep
14-
class MapSnapshot private constructor(val nativePtr: Long, bitmap: Bitmap, attributions: Array<String>, showLogo: Boolean) {
14+
class MapSnapshot private constructor(val nativePtr: Long, bitmap: Bitmap, attributions: Array<String>, showLogo: Boolean, showAttribution: Boolean) {
1515

1616
/**
1717
* @return the large
@@ -28,13 +28,19 @@ class MapSnapshot private constructor(val nativePtr: Long, bitmap: Bitmap, attri
2828
*/
2929
val isShowLogo: Boolean
3030

31+
/**
32+
* @return Flag indicating to show the attribution.
33+
*/
34+
val isShowAttribution: Boolean
35+
3136
/**
3237
* Created from native side
3338
*/
3439
init {
3540
this.bitmap = bitmap
3641
this.attributions = attributions
3742
isShowLogo = showLogo
43+
isShowAttribution = showAttribution
3844
}
3945

4046
/**

platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ open class MapSnapshotter(context: Context, options: Options) {
134134

135135
var showLogo = true
136136

137+
var showAttribution = true
138+
137139
/**
138140
* @return the font family used for locally generating ideographs,
139141
* Default font for local ideograph font family is [MapLibreConstants.DEFAULT_FONT].
@@ -231,6 +233,15 @@ open class MapSnapshotter(context: Context, options: Options) {
231233
return this
232234
}
233235

236+
/**
237+
* @param showAttribution The flag indicating to show the attribution.
238+
* @return the mutated [Options]
239+
*/
240+
fun withAttribution(showAttribution: Boolean): Options {
241+
this.showAttribution = showAttribution
242+
return this
243+
}
244+
234245
/**
235246
* Set the font family for generating glyphs locally for ideographs in the &#x27;CJK Unified Ideographs&#x27;
236247
* and &#x27;Hangul Syllables&#x27; ranges.
@@ -337,6 +348,7 @@ open class MapSnapshotter(context: Context, options: Options) {
337348
options.region,
338349
options.cameraPosition,
339350
options.showLogo,
351+
options.showAttribution,
340352
options.localIdeographFontFamily
341353
)
342354
}
@@ -476,7 +488,7 @@ open class MapSnapshotter(context: Context, options: Options) {
476488
*/
477489
protected open fun addOverlay(mapSnapshot: MapSnapshot) {
478490
val snapshot = mapSnapshot.bitmap
479-
val canvas = Canvas(snapshot!!)
491+
val canvas = Canvas(snapshot)
480492
val margin = context.resources.displayMetrics.density.toInt() * LOGO_MARGIN_DP
481493
drawOverlay(mapSnapshot, snapshot, canvas, margin)
482494
}
@@ -511,6 +523,7 @@ open class MapSnapshotter(context: Context, options: Options) {
511523

512524
private fun drawAttribution(mapSnapshot: MapSnapshot, canvas: Canvas, measure: AttributionMeasure, layout: AttributionLayout?) {
513525
// draw attribution
526+
if (!mapSnapshot.isShowAttribution) return
514527
val anchorPoint = layout!!.anchorPoint
515528
if (anchorPoint != null) {
516529
drawAttribution(canvas, measure, anchorPoint)
@@ -732,6 +745,7 @@ open class MapSnapshotter(context: Context, options: Options) {
732745
region: LatLngBounds?,
733746
position: CameraPosition?,
734747
showLogo: Boolean,
748+
showAttribution: Boolean,
735749
localIdeographFontFamily: String?
736750
)
737751

0 commit comments

Comments
 (0)