[android] map snapshotter - expose attributions

This commit is contained in:
Ivo van Dongen
2017-10-27 20:08:47 +03:00
committed by Ivo van Dongen
parent 2d81186fc1
commit e0a305247a
5 changed files with 48 additions and 5 deletions

View File

@@ -14,13 +14,15 @@ public class MapSnapshot {
private long nativePtr = 0;
private Bitmap bitmap;
private String[] attributions;
/**
* Created from native side
*/
private MapSnapshot(long nativePtr, Bitmap bitmap) {
private MapSnapshot(long nativePtr, Bitmap bitmap, String[] attributions) {
this.nativePtr = nativePtr;
this.bitmap = bitmap;
this.attributions = attributions;
}
/**
@@ -38,6 +40,13 @@ public class MapSnapshot {
*/
public native PointF pixelForLatLng(LatLng latLng);
/**
* @return The attributions for the sources of this snapshot.
*/
protected String[] getAttributions() {
return attributions;
}
// Unused, needed for peer binding
private native void initialize();

View File

@@ -0,0 +1,26 @@
#pragma once
#include <jni/jni.hpp>
#include <string>
#include <vector>
namespace jni {
inline Array<String> MakeAnything(ThingToMake<Array<String>>, JNIEnv& env, const std::vector<std::string>& vector)
{
static auto clazz = *Class<StringTag>::Find(env).NewGlobalRef(env).release();
auto result = Array<String>::New(env, vector.size(), clazz);
std::size_t index = 0;
for (auto&& item : vector) {
auto element = Make<jni::String>(env, item);
result.Set(env, index, element);
DeleteLocalRef(env, element);
index++;
}
return result;
}
}

View File

@@ -1,6 +1,7 @@
#include "map_snapshot.hpp"
#include "../bitmap.hpp"
#include "../jni/collection.hpp"
#include <memory>
@@ -25,14 +26,15 @@ jni::Object<PointF> MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object<La
jni::Object<MapSnapshot> MapSnapshot::New(JNIEnv& env,
PremultipliedImage&& image,
float pixelRatio,
std::vector<std::string> attributions,
mbgl::MapSnapshotter::PointForFn pointForFn) {
// Create the bitmap
auto bitmap = Bitmap::CreateBitmap(env, std::move(image));
// Create the Mapsnapshot peers
static auto constructor = javaClass.GetConstructor<jni::jlong, jni::Object<Bitmap>>(env);
static auto constructor = javaClass.GetConstructor<jni::jlong, jni::Object<Bitmap>, jni::Array<jni::String>>(env);
auto nativePeer = std::make_unique<MapSnapshot>(pixelRatio, pointForFn);
return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap);
return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap, jni::Make<jni::Array<jni::String>>(env, attributions));
}
jni::Class<MapSnapshot> MapSnapshot::javaClass;

View File

@@ -7,6 +7,9 @@
#include "../geometry/lat_lng.hpp"
#include "../graphics/pointf.hpp"
#include <vector>
#include <string>
namespace mbgl {
namespace android {
@@ -22,6 +25,7 @@ public:
static jni::Object<MapSnapshot> New(JNIEnv& env,
PremultipliedImage&& image,
float pixelRatio,
std::vector<std::string> attributions,
PointForFn pointForFn);
MapSnapshot(jni::JNIEnv&) {};

View File

@@ -58,7 +58,9 @@ MapSnapshotter::~MapSnapshotter() = default;
void MapSnapshotter::start(JNIEnv&) {
MBGL_VERIFY_THREAD(tid);
snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image, mbgl::MapSnapshotter::PointForFn pointForFn) {
snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(
*Scheduler::GetCurrent(),
[this](std::exception_ptr err, PremultipliedImage image, std::vector<std::string> attributions, mbgl::MapSnapshotter::PointForFn pointForFn) {
MBGL_VERIFY_THREAD(tid);
android::UniqueEnv _env = android::AttachEnv();
@@ -68,7 +70,7 @@ void MapSnapshotter::start(JNIEnv&) {
javaPeer->Call(*_env, onSnapshotFailed, jni::Make<jni::String>(*_env, util::toString(err)));
} else {
// Create the wrapper
auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, pointForFn);
auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, attributions, pointForFn);
// invoke callback
static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady");