From 9d702a806fba61cfbead347ec87373749a435f97 Mon Sep 17 00:00:00 2001 From: tobrun Date: Thu, 28 Jun 2018 11:27:04 +0200 Subject: [android] [core] - add query rendered features to map snapshot --- platform/android/src/snapshotter/map_snapshot.cpp | 26 +++++++++++++++++++--- platform/android/src/snapshotter/map_snapshot.hpp | 12 +++++++++- .../android/src/snapshotter/map_snapshotter.cpp | 4 ++-- platform/default/mbgl/gl/headless_frontend.cpp | 2 ++ platform/default/mbgl/gl/headless_frontend.hpp | 3 +++ platform/default/mbgl/map/map_snapshotter.cpp | 6 +++++ platform/default/mbgl/map/map_snapshotter.hpp | 4 ++++ 7 files changed, 51 insertions(+), 6 deletions(-) diff --git a/platform/android/src/snapshotter/map_snapshot.cpp b/platform/android/src/snapshotter/map_snapshot.cpp index f5092b3c56..1e86586ad3 100644 --- a/platform/android/src/snapshotter/map_snapshot.cpp +++ b/platform/android/src/snapshotter/map_snapshot.cpp @@ -2,6 +2,9 @@ #include "../bitmap.hpp" #include "../jni/collection.hpp" +#include "../geojson/feature.hpp" +#include "../conversion/collection.hpp" +#include "../style/conversion/filter.hpp" #include @@ -20,6 +23,20 @@ jni::Object MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object> MapSnapshot::queryRenderedFeaturesForPoint(JNIEnv& env, jni::jfloat x, jni::jfloat y, + jni::Array layerIds, + jni::Array> jfilter) { + using namespace mbgl::android::conversion; + using namespace mbgl::android::geojson; + + mbgl::optional> layers; + if (layerIds != nullptr && layerIds.Length(env) > 0) { + layers = android::conversion::toVector(env, layerIds); + } + mapbox::geometry::point point = {x, y}; + std::vector features = queryPointForFn(point, {layers, toFilter(env, jfilter)}); + return *convert>, std::vector>(env, features); +} // Static methods // @@ -28,13 +45,14 @@ jni::Object MapSnapshot::New(JNIEnv& env, float pixelRatio, std::vector attributions, bool showLogo, - mbgl::MapSnapshotter::PointForFn pointForFn) { + mbgl::MapSnapshotter::PointForFn pointForFn, + mbgl::MapSnapshotter::QueryPointForFn queryPointForFn) { // 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, queryPointForFn); return javaClass.New(env, constructor, reinterpret_cast(nativePeer.release()), bitmap, jni::Make>(env, attributions), (jni::jboolean) showLogo); } @@ -52,7 +70,9 @@ void MapSnapshot::registerNative(jni::JNIEnv& env) { std::make_unique, "initialize", "finalize", - METHOD(&MapSnapshot::pixelForLatLng, "pixelForLatLng") + METHOD(&MapSnapshot::pixelForLatLng, "pixelForLatLng"), + METHOD(&MapSnapshot::queryRenderedFeaturesForPoint, "queryRenderedFeaturesForPoint")//, +// METHOD(&MapSnapshot::queryRenderedFeaturesForBox, "queryRenderedFeaturesForPoint") ); } diff --git a/platform/android/src/snapshotter/map_snapshot.hpp b/platform/android/src/snapshotter/map_snapshot.hpp index 4673dcd16e..3393d66317 100644 --- a/platform/android/src/snapshotter/map_snapshot.hpp +++ b/platform/android/src/snapshotter/map_snapshot.hpp @@ -6,6 +6,7 @@ #include "../geometry/lat_lng.hpp" #include "../graphics/pointf.hpp" +#include "../geojson/feature.hpp" #include #include @@ -17,6 +18,7 @@ class MapSnapshot { public: using PointForFn = mbgl::MapSnapshotter::PointForFn; + using QueryPointForFn = mbgl::MapSnapshotter::QueryPointForFn; static constexpr auto Name() { return "com/mapbox/mapboxsdk/snapshotter/MapSnapshot"; }; @@ -27,19 +29,27 @@ public: float pixelRatio, std::vector attributions, bool showLogo, - PointForFn pointForFn); + PointForFn pointForFn, + QueryPointForFn queryPointForFn); MapSnapshot(jni::JNIEnv&) {}; MapSnapshot(float pixelRatio, PointForFn); ~MapSnapshot(); jni::Object pixelForLatLng(jni::JNIEnv&, jni::Object); + jni::Array> queryRenderedFeaturesForPoint(JNIEnv&, jni::jfloat, jni::jfloat, + jni::Array, + jni::Array> jfilter); +// jni::Array> queryRenderedFeaturesForBox(JNIEnv&, jni::jfloat, jni::jfloat, jni::jfloat, +// jni::jfloat, jni::Array, +// jni::Array> jfilter); private: static jni::Class javaClass; float pixelRatio; mbgl::MapSnapshotter::PointForFn pointForFn; + mbgl::MapSnapshotter::QueryPointForFn queryPointForFn; }; } // namespace android diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index 155fdf81fb..e0e7de5263 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::QueryPointForFn queryPointForFn) { 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, queryPointForFn); // invoke callback static auto onSnapshotReady = javaClass.GetMethod)>(*_env, "onSnapshotReady"); diff --git a/platform/default/mbgl/gl/headless_frontend.cpp b/platform/default/mbgl/gl/headless_frontend.cpp index 4263d2b148..d8d316ad4f 100644 --- a/platform/default/mbgl/gl/headless_frontend.cpp +++ b/platform/default/mbgl/gl/headless_frontend.cpp @@ -91,6 +91,8 @@ optional HeadlessFrontend::getTransformState() const { } else { return {}; } + + } } // namespace mbgl diff --git a/platform/default/mbgl/gl/headless_frontend.hpp b/platform/default/mbgl/gl/headless_frontend.hpp index 8ae617d37b..8bc0edcb4a 100644 --- a/platform/default/mbgl/gl/headless_frontend.hpp +++ b/platform/default/mbgl/gl/headless_frontend.hpp @@ -7,6 +7,8 @@ #include #include +#include +#include namespace mbgl { @@ -37,6 +39,7 @@ public: PremultipliedImage render(Map&); optional getTransformState() const; + std::vector queryRenderedFeatures(const ScreenCoordinate&, const RenderedQueryOptions&) const; private: Size size; diff --git a/platform/default/mbgl/map/map_snapshotter.cpp b/platform/default/mbgl/map/map_snapshotter.cpp index a909e3fe9b..0c5c4c8d48 100644 --- a/platform/default/mbgl/map/map_snapshotter.cpp +++ b/platform/default/mbgl/map/map_snapshotter.cpp @@ -85,6 +85,12 @@ void MapSnapshotter::Impl::snapshot(ActorRef callback) return transform.latLngToScreenCoordinate(unwrappedLatLng); }}; + assert (frontend.getRenderer()); + QueryPointForFn queryPointForFn { [=] (const mapbox::geometry::point& point, + const RenderedQueryOptions& renderedQueryOptions){ + return frontend.getRenderer()->queryRenderedFeatures({point.x, point.y}, renderedQueryOptions); + }}; + // Collect all source attributions std::vector attributions; for (auto source : map.getStyle().getSources()) { diff --git a/platform/default/mbgl/map/map_snapshotter.hpp b/platform/default/mbgl/map/map_snapshotter.hpp index b9e6307664..a633a4b16c 100644 --- a/platform/default/mbgl/map/map_snapshotter.hpp +++ b/platform/default/mbgl/map/map_snapshotter.hpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include namespace mbgl { @@ -52,6 +54,8 @@ public: LatLngBounds getRegion() const; using PointForFn = std::function; + using QueryPointForFn = std::function (const mapbox::geometry::point&, + const RenderedQueryOptions&)>; using Attributions = std::vector; using Callback = std::function; void snapshot(ActorRef); -- cgit v1.2.1