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

Commit 4a89384

Browse files
committed
[android] Expose map snapshot attributions
1 parent f3f3f52 commit 4a89384

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshot.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ public class MapSnapshot {
1414

1515
private long nativePtr = 0;
1616
private Bitmap bitmap;
17+
private String[] attributions;
1718

1819
/**
1920
* Created from native side
2021
*/
21-
private MapSnapshot(long nativePtr, Bitmap bitmap) {
22+
private MapSnapshot(long nativePtr, Bitmap bitmap, String[] attributions) {
2223
this.nativePtr = nativePtr;
2324
this.bitmap = bitmap;
25+
this.attributions = attributions;
2426
}
2527

2628
/**
@@ -38,6 +40,13 @@ public Bitmap getBitmap() {
3840
*/
3941
public native PointF pixelForLatLng(LatLng latLng);
4042

43+
/**
44+
* @return The attributions for the sources of this snapshot.
45+
*/
46+
protected String[] getAttributions() {
47+
return attributions;
48+
}
49+
4150
// Unused, needed for peer binding
4251
private native void initialize();
4352

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <jni/jni.hpp>
4+
5+
#include <string>
6+
#include <vector>
7+
8+
namespace jni {
9+
10+
inline Array<String> MakeAnything(ThingToMake<Array<String>>, JNIEnv& env, const std::vector<std::string>& vector)
11+
{
12+
static auto clazz = *Class<StringTag>::Find(env).NewGlobalRef(env).release();
13+
auto result = Array<String>::New(env, vector.size(), clazz);
14+
15+
std::size_t index = 0;
16+
for (auto&& item : vector) {
17+
auto element = Make<jni::String>(env, item);
18+
result.Set(env, index, element);
19+
DeleteLocalRef(env, element);
20+
index++;
21+
}
22+
23+
return result;
24+
}
25+
26+
}

platform/android/src/snapshotter/map_snapshot.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "map_snapshot.hpp"
22

33
#include "../bitmap.hpp"
4+
#include "../jni/collection.hpp"
45

56
#include <memory>
67

@@ -25,14 +26,15 @@ jni::Object<PointF> MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object<La
2526
jni::Object<MapSnapshot> MapSnapshot::New(JNIEnv& env,
2627
PremultipliedImage&& image,
2728
float pixelRatio,
29+
std::vector<std::string> attributions,
2830
mbgl::MapSnapshotter::PointForFn pointForFn) {
2931
// Create the bitmap
3032
auto bitmap = Bitmap::CreateBitmap(env, std::move(image));
3133

3234
// Create the Mapsnapshot peers
33-
static auto constructor = javaClass.GetConstructor<jni::jlong, jni::Object<Bitmap>>(env);
35+
static auto constructor = javaClass.GetConstructor<jni::jlong, jni::Object<Bitmap>, jni::Array<jni::String>>(env);
3436
auto nativePeer = std::make_unique<MapSnapshot>(pixelRatio, pointForFn);
35-
return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap);
37+
return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap, jni::Make<jni::Array<jni::String>>(env, attributions));
3638
}
3739

3840
jni::Class<MapSnapshot> MapSnapshot::javaClass;

platform/android/src/snapshotter/map_snapshot.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include "../geometry/lat_lng.hpp"
88
#include "../graphics/pointf.hpp"
99

10+
#include <vector>
11+
#include <string>
12+
1013
namespace mbgl {
1114
namespace android {
1215

@@ -22,6 +25,7 @@ class MapSnapshot {
2225
static jni::Object<MapSnapshot> New(JNIEnv& env,
2326
PremultipliedImage&& image,
2427
float pixelRatio,
28+
std::vector<std::string> attributions,
2529
PointForFn pointForFn);
2630

2731
MapSnapshot(jni::JNIEnv&) {};

platform/android/src/snapshotter/map_snapshotter.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ MapSnapshotter::~MapSnapshotter() = default;
5858
void MapSnapshotter::start(JNIEnv&) {
5959
MBGL_VERIFY_THREAD(tid);
6060

61-
snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image, mbgl::MapSnapshotter::PointForFn pointForFn) {
61+
snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(
62+
*Scheduler::GetCurrent(),
63+
[this](std::exception_ptr err, PremultipliedImage image, std::vector<std::string> attributions, mbgl::MapSnapshotter::PointForFn pointForFn) {
6264
MBGL_VERIFY_THREAD(tid);
6365
android::UniqueEnv _env = android::AttachEnv();
6466

@@ -68,7 +70,7 @@ void MapSnapshotter::start(JNIEnv&) {
6870
javaPeer->Call(*_env, onSnapshotFailed, jni::Make<jni::String>(*_env, util::toString(err)));
6971
} else {
7072
// Create the wrapper
71-
auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, pointForFn);
73+
auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, attributions, pointForFn);
7274

7375
// invoke callback
7476
static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady");

0 commit comments

Comments
 (0)