summaryrefslogtreecommitdiff
path: root/platform/android/src/snapshotter/map_snapshot.cpp
blob: 42c1c16ca17af803beb095c234d10e808d8a3944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "map_snapshot.hpp"

#include "../bitmap.hpp"
#include "../jni/collection.hpp"

#include <memory>

namespace mbgl {
namespace android {

MapSnapshot::MapSnapshot(float pixelRatio_, MapSnapshot::PointForFn pointForFn_, MapSnapshot::LatLngForFn latLngForFn_)
    : pixelRatio(pixelRatio_)
    , pointForFn(std::move(pointForFn_))
    , latLngForFn(std::move(latLngForFn_)) {
}

MapSnapshot::~MapSnapshot() = default;

jni::Object<PointF> MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object<LatLng> jLatLng) {
    ScreenCoordinate point = pointForFn(LatLng::getLatLng(env, jLatLng));
    return PointF::New(env, point.x * pixelRatio, point.y * pixelRatio);
}

jni::Object<LatLng> MapSnapshot::latLngForPixel(jni::JNIEnv& env, jni::Object<PointF>jPoint) {
    return LatLng::New(env, latLngForFn(PointF::getScreenCoordinate(env, jPoint)));
}

// Static methods //

jni::Object<MapSnapshot> MapSnapshot::New(JNIEnv& env,
                                          PremultipliedImage&& image,
                                          float pixelRatio,
                                          std::vector<std::string> attributions,
                                          bool showLogo,
                                          mbgl::MapSnapshotter::PointForFn pointForFn,
                                          mbgl::MapSnapshotter::LatLngForFn latLngForFn) {
    // 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>, jni::Array<jni::String>, jni::jboolean>(env);
    auto nativePeer = std::make_unique<MapSnapshot>(pixelRatio, pointForFn, latLngForFn);
    return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap, jni::Make<jni::Array<jni::String>>(env, attributions), (jni::jboolean) showLogo);
}

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

void MapSnapshot::registerNative(jni::JNIEnv& env) {
    // Lookup the class
    MapSnapshot::javaClass = *jni::Class<MapSnapshot>::Find(env).NewGlobalRef(env).release();

#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)

    // Register the peer
    jni::RegisterNativePeer<MapSnapshot>(env, MapSnapshot::javaClass,
                                            "nativePtr",
                                            std::make_unique<MapSnapshot, JNIEnv&>,
                                            "initialize",
                                            "finalize",
                                            METHOD(&MapSnapshot::latLngForPixel, "latLngForPixel"),
                                            METHOD(&MapSnapshot::pixelForLatLng, "pixelForLatLng")
    );
}

} // namespace android
} // namespace mbgl