summaryrefslogtreecommitdiff
path: root/platform/android/src/map/camera_position.cpp
blob: 01ffc6530b0395e53e8227b8221c7caad102ec1f (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
#include "camera_position.hpp"
#include "../geometry/lat_lng.hpp"

namespace mbgl {
namespace android {

jni::Object<CameraPosition> CameraPosition::New(jni::JNIEnv &env, mbgl::CameraOptions options) {
    static auto constructor = CameraPosition::javaClass.GetConstructor<jni::Object<LatLng>, double, double, double>(env);

    // wrap LatLng values coming from core
    auto center = options.center.value();
    center.wrap();

    // convert bearing, measured in radians counterclockwise from true north.
    // Wrapped to [−π rad, π rad). Android binding from 0 to 360 degrees
    double bearing_degrees = -options.angle.value_or(0) * util::RAD2DEG;
    while (bearing_degrees > 360) {
        bearing_degrees -= 360;
    }
    while (bearing_degrees < 0) {
        bearing_degrees += 360;
    }

    // convert tilt, core ranges from  [0 rad, 1,0472 rad], android ranges from 0 to 60
    double tilt_degrees = options.pitch.value_or(0) * util::RAD2DEG;

    return CameraPosition::javaClass.New(env, constructor, LatLng::New(env, center), options.zoom.value_or(0), tilt_degrees, bearing_degrees);
}

mbgl::CameraOptions CameraPosition::getCameraOptions(jni::JNIEnv& env, jni::Object<CameraPosition> position) {
    static auto bearing = CameraPosition::javaClass.GetField<jni::jdouble>(env, "bearing");
    static auto target = CameraPosition::javaClass.GetField<jni::Object<LatLng>>(env, "target");
    static auto tilt = CameraPosition::javaClass.GetField<jni::jdouble>(env, "tilt");
    static auto zoom = CameraPosition::javaClass.GetField<jni::jdouble>(env, "zoom");

    auto jtarget = position.Get(env, target);
    auto center = LatLng::getLatLng(env, jtarget);
    jni::DeleteLocalRef(env, jtarget);

    return mbgl::CameraOptions {
            center,
            {},
            {},
            position.Get(env, zoom),
            position.Get(env, bearing) * util::DEG2RAD,
            position.Get(env, tilt)
    };
}

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

jni::Class<CameraPosition> CameraPosition::javaClass;


} // namespace android
} // namespace mb