From c6f1d619ca12bfdcf6aaaeafc4e0399cd38283c7 Mon Sep 17 00:00:00 2001 From: tobrun Date: Tue, 26 Jun 2018 18:07:18 +0200 Subject: [core] - add LatLng for screencoordinate to mapsnapshotter --- platform/android/src/graphics/pointf.cpp | 7 +++++++ platform/android/src/graphics/pointf.hpp | 2 ++ platform/android/src/snapshotter/map_snapshot.cpp | 14 ++++++++------ platform/android/src/snapshotter/map_snapshot.hpp | 7 +++++-- platform/android/src/snapshotter/map_snapshotter.cpp | 4 ++-- platform/default/mbgl/map/map_snapshotter.cpp | 12 +++++++++++- platform/default/mbgl/map/map_snapshotter.hpp | 3 ++- 7 files changed, 37 insertions(+), 12 deletions(-) diff --git a/platform/android/src/graphics/pointf.cpp b/platform/android/src/graphics/pointf.cpp index 6e91b81416..3f854da2ea 100644 --- a/platform/android/src/graphics/pointf.cpp +++ b/platform/android/src/graphics/pointf.cpp @@ -1,3 +1,4 @@ +#include #include "pointf.hpp" namespace mbgl { @@ -8,6 +9,12 @@ jni::Object PointF::New(jni::JNIEnv& env, float x, float y) { return PointF::javaClass.New(env, constructor, x, y); } +mbgl::ScreenCoordinate PointF::getScreenCoordinate(jni::JNIEnv& env, jni::Object point) { + static auto xField = PointF::javaClass.GetField(env, "x"); + static auto yField = PointF::javaClass.GetField(env, "y"); + return mbgl::ScreenCoordinate{point.Get(env, xField), point.Get(env, yField)}; +} + void PointF::registerNative(jni::JNIEnv& env) { // Lookup the class PointF::javaClass = *jni::Class::Find(env).NewGlobalRef(env).release(); diff --git a/platform/android/src/graphics/pointf.hpp b/platform/android/src/graphics/pointf.hpp index ea25ad2b40..e3ef24dd65 100644 --- a/platform/android/src/graphics/pointf.hpp +++ b/platform/android/src/graphics/pointf.hpp @@ -14,6 +14,8 @@ public: static jni::Object New(jni::JNIEnv&, float, float); + static mbgl::ScreenCoordinate getScreenCoordinate(jni::JNIEnv&, jni::Object); + static jni::Class javaClass; static void registerNative(jni::JNIEnv&); diff --git a/platform/android/src/snapshotter/map_snapshot.cpp b/platform/android/src/snapshotter/map_snapshot.cpp index bbbf7cc207..42c1c16ca1 100644 --- a/platform/android/src/snapshotter/map_snapshot.cpp +++ b/platform/android/src/snapshotter/map_snapshot.cpp @@ -8,9 +8,10 @@ namespace mbgl { namespace android { -MapSnapshot::MapSnapshot(float pixelRatio_, MapSnapshot::PointForFn pointForFn_) +MapSnapshot::MapSnapshot(float pixelRatio_, MapSnapshot::PointForFn pointForFn_, MapSnapshot::LatLngForFn latLngForFn_) : pixelRatio(pixelRatio_) - , pointForFn(std::move(pointForFn_)) { + , pointForFn(std::move(pointForFn_)) + , latLngForFn(std::move(latLngForFn_)) { } MapSnapshot::~MapSnapshot() = default; @@ -20,8 +21,8 @@ jni::Object MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object MapSnapshot::latLngForPixel(jni::JNIEnv& env, jni::Object) { - return LatLng::New(env, {0, 0}); +jni::Object MapSnapshot::latLngForPixel(jni::JNIEnv& env, jni::ObjectjPoint) { + return LatLng::New(env, latLngForFn(PointF::getScreenCoordinate(env, jPoint))); } // Static methods // @@ -31,13 +32,14 @@ jni::Object MapSnapshot::New(JNIEnv& env, float pixelRatio, std::vector attributions, bool showLogo, - mbgl::MapSnapshotter::PointForFn pointForFn) { + 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::Array, jni::jboolean>(env); - auto nativePeer = std::make_unique(pixelRatio, pointForFn); + auto nativePeer = std::make_unique(pixelRatio, pointForFn, latLngForFn); return javaClass.New(env, constructor, reinterpret_cast(nativePeer.release()), bitmap, jni::Make>(env, attributions), (jni::jboolean) showLogo); } diff --git a/platform/android/src/snapshotter/map_snapshot.hpp b/platform/android/src/snapshotter/map_snapshot.hpp index 48dd1b6049..f168be85b4 100644 --- a/platform/android/src/snapshotter/map_snapshot.hpp +++ b/platform/android/src/snapshotter/map_snapshot.hpp @@ -17,6 +17,7 @@ class MapSnapshot { public: using PointForFn = mbgl::MapSnapshotter::PointForFn; + using LatLngForFn = mbgl::MapSnapshotter::LatLngForFn; static constexpr auto Name() { return "com/mapbox/mapboxsdk/snapshotter/MapSnapshot"; }; @@ -27,10 +28,11 @@ public: float pixelRatio, std::vector attributions, bool showLogo, - PointForFn pointForFn); + PointForFn pointForFn, + LatLngForFn latLngForFn); MapSnapshot(jni::JNIEnv&) {}; - MapSnapshot(float pixelRatio, PointForFn); + MapSnapshot(float pixelRatio, PointForFn, LatLngForFn); ~MapSnapshot(); jni::Object pixelForLatLng(jni::JNIEnv&, jni::Object); @@ -41,6 +43,7 @@ private: float pixelRatio; mbgl::MapSnapshotter::PointForFn pointForFn; + mbgl::MapSnapshotter::LatLngForFn latLngForFn; }; } // namespace android diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index 155fdf81fb..fae941874e 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -77,7 +77,7 @@ void MapSnapshotter::start(JNIEnv& env) { snapshotCallback = std::make_unique>( *Scheduler::GetCurrent(), - [this](std::exception_ptr err, PremultipliedImage image, std::vector attributions, mbgl::MapSnapshotter::PointForFn pointForFn) { + [this](std::exception_ptr err, PremultipliedImage image, std::vector attributions, mbgl::MapSnapshotter::PointForFn pointForFn, mbgl::MapSnapshotter::LatLngForFn latLngForFn) { MBGL_VERIFY_THREAD(tid); android::UniqueEnv _env = android::AttachEnv(); @@ -89,7 +89,7 @@ void MapSnapshotter::start(JNIEnv& env) { jni::DeleteLocalRef(*_env, message); } else { // Create the wrapper - auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, attributions, showLogo, pointForFn); + auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, attributions, showLogo, pointForFn, latLngForFn); // invoke callback static auto onSnapshotReady = javaClass.GetMethod)>(*_env, "onSnapshotReady"); diff --git a/platform/default/mbgl/map/map_snapshotter.cpp b/platform/default/mbgl/map/map_snapshotter.cpp index a909e3fe9b..0f65e2d924 100644 --- a/platform/default/mbgl/map/map_snapshotter.cpp +++ b/platform/default/mbgl/map/map_snapshotter.cpp @@ -85,6 +85,15 @@ void MapSnapshotter::Impl::snapshot(ActorRef callback) return transform.latLngToScreenCoordinate(unwrappedLatLng); }}; + // Create lambda that captures the current transform state + // and can be used to translate for geographic to screen + // coordinates + assert (frontend.getTransformState()); + LatLngForFn latLngForFn { [=, transformState = *frontend.getTransformState()] (const ScreenCoordinate& screenCoordinate) { + Transform transform { transformState }; + return transform.screenCoordinateToLatLng(screenCoordinate); + }}; + // Collect all source attributions std::vector attributions; for (auto source : map.getStyle().getSources()) { @@ -100,7 +109,8 @@ void MapSnapshotter::Impl::snapshot(ActorRef callback) error, error ? PremultipliedImage() : frontend.readStillImage(), std::move(attributions), - std::move(pointForFn) + std::move(pointForFn), + std::move(latLngForFn) ); }); } diff --git a/platform/default/mbgl/map/map_snapshotter.hpp b/platform/default/mbgl/map/map_snapshotter.hpp index b9e6307664..f2e571c686 100644 --- a/platform/default/mbgl/map/map_snapshotter.hpp +++ b/platform/default/mbgl/map/map_snapshotter.hpp @@ -52,8 +52,9 @@ public: LatLngBounds getRegion() const; using PointForFn = std::function; + using LatLngForFn = std::function; using Attributions = std::vector; - using Callback = std::function; + using Callback = std::function; void snapshot(ActorRef); private: -- cgit v1.2.1