Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] - add LatLng for screencoordinate to mapsnapshotter
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jul 5, 2018
1 parent 473dd9b commit 99f9787
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
7 changes: 7 additions & 0 deletions platform/android/src/graphics/pointf.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <mbgl/util/geo.hpp>
#include "pointf.hpp"

namespace mbgl {
Expand All @@ -8,6 +9,12 @@ jni::Object<PointF> 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<PointF> point) {
static auto xField = PointF::javaClass.GetField<jni::jfloat>(env, "x");
static auto yField = PointF::javaClass.GetField<jni::jfloat>(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<PointF>::Find(env).NewGlobalRef(env).release();
Expand Down
2 changes: 2 additions & 0 deletions platform/android/src/graphics/pointf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class PointF : private mbgl::util::noncopyable {

static jni::Object<PointF> New(jni::JNIEnv&, float, float);

static mbgl::ScreenCoordinate getScreenCoordinate(jni::JNIEnv&, jni::Object<PointF>);

static jni::Class<PointF> javaClass;

static void registerNative(jni::JNIEnv&);
Expand Down
14 changes: 8 additions & 6 deletions platform/android/src/snapshotter/map_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,8 +21,8 @@ jni::Object<PointF> MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object<La
return PointF::New(env, point.x * pixelRatio, point.y * pixelRatio);
}

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

// Static methods //
Expand All @@ -31,13 +32,14 @@ jni::Object<MapSnapshot> MapSnapshot::New(JNIEnv& env,
float pixelRatio,
std::vector<std::string> 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::jlong, jni::Object<Bitmap>, jni::Array<jni::String>, jni::jboolean>(env);
auto nativePeer = std::make_unique<MapSnapshot>(pixelRatio, pointForFn);
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);
}

Expand Down
7 changes: 5 additions & 2 deletions platform/android/src/snapshotter/map_snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"; };

Expand All @@ -27,10 +28,11 @@ class MapSnapshot {
float pixelRatio,
std::vector<std::string> attributions,
bool showLogo,
PointForFn pointForFn);
PointForFn pointForFn,
LatLngForFn latLngForFn);

MapSnapshot(jni::JNIEnv&) {};
MapSnapshot(float pixelRatio, PointForFn);
MapSnapshot(float pixelRatio, PointForFn, LatLngForFn);
~MapSnapshot();

jni::Object<PointF> pixelForLatLng(jni::JNIEnv&, jni::Object<LatLng>);
Expand All @@ -41,6 +43,7 @@ class MapSnapshot {

float pixelRatio;
mbgl::MapSnapshotter::PointForFn pointForFn;
mbgl::MapSnapshotter::LatLngForFn latLngForFn;
};

} // namespace android
Expand Down
4 changes: 2 additions & 2 deletions platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void MapSnapshotter::start(JNIEnv& env) {

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) {
[this](std::exception_ptr err, PremultipliedImage image, std::vector<std::string> attributions, mbgl::MapSnapshotter::PointForFn pointForFn, mbgl::MapSnapshotter::LatLngForFn latLngForFn) {
MBGL_VERIFY_THREAD(tid);
android::UniqueEnv _env = android::AttachEnv();

Expand All @@ -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<void (jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady");
Expand Down
12 changes: 11 additions & 1 deletion platform/default/mbgl/map/map_snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ void MapSnapshotter::Impl::snapshot(ActorRef<MapSnapshotter::Callback> 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<std::string> attributions;
for (auto source : map.getStyle().getSources()) {
Expand All @@ -102,7 +111,8 @@ void MapSnapshotter::Impl::snapshot(ActorRef<MapSnapshotter::Callback> callback)
error,
error ? PremultipliedImage() : frontend.readStillImage(),
std::move(attributions),
std::move(pointForFn)
std::move(pointForFn),
std::move(latLngForFn)
);
});
}
Expand Down
3 changes: 2 additions & 1 deletion platform/default/mbgl/map/map_snapshotter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class MapSnapshotter {
LatLngBounds getRegion() const;

using PointForFn = std::function<ScreenCoordinate (const LatLng&)>;
using LatLngForFn = std::function<LatLng (const ScreenCoordinate&)>;
using Attributions = std::vector<std::string>;
using Callback = std::function<void (std::exception_ptr, PremultipliedImage, Attributions, PointForFn)>;
using Callback = std::function<void (std::exception_ptr, PremultipliedImage, Attributions, PointForFn, LatLngForFn)>;
void snapshot(ActorRef<Callback>);

private:
Expand Down

0 comments on commit 99f9787

Please sign in to comment.