[default] map snapshotter - add function to get screen coordinate from latlng for snapshot

- Wraps the TransformState for the snapshot so that the snapshotter itself is free to be re-used or destroyed
This commit is contained in:
Ivo van Dongen
2017-10-09 16:49:29 +03:00
committed by Fabian Guerra Soto
parent df7755fcd9
commit b3834447ff
2 changed files with 23 additions and 2 deletions

View File

@@ -3,9 +3,11 @@
#include <mbgl/actor/actor_ref.hpp>
#include <mbgl/gl/headless_frontend.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/util/event.hpp>
#include <mbgl/map/transform.hpp>
namespace mbgl {
@@ -62,7 +64,24 @@ MapSnapshotter::Impl::Impl(FileSource& fileSource,
void MapSnapshotter::Impl::snapshot(ActorRef<MapSnapshotter::Callback> callback) {
map.renderStill([this, callback = std::move(callback)] (std::exception_ptr error) mutable {
callback.invoke(&MapSnapshotter::Callback::operator(), error, error ? PremultipliedImage() : frontend.readStillImage());
// Create lambda that captures the current transform state
// and can be used to translate for geographic to screen
// coordinates
assert (frontend.getTransformState());
PointForFn pointForFn { [=, center=map.getLatLng(), transformState = *frontend.getTransformState()] (const LatLng& latLng) {
LatLng unwrappedLatLng = latLng.wrapped();
unwrappedLatLng.unwrapForShortestPath(center);
Transform transform { transformState };
return transform.latLngToScreenCoordinate(unwrappedLatLng);
}};
callback.invoke(
&MapSnapshotter::Callback::operator(),
error,
error ? PremultipliedImage() : frontend.readStillImage(),
std::move(pointForFn)
);
});
}

View File

@@ -3,6 +3,7 @@
#include <mbgl/util/image.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/geo.hpp>
#include <exception>
#include <memory>
@@ -46,7 +47,8 @@ public:
void setRegion(const LatLngBounds&);
LatLngBounds getRegion() const;
using Callback = std::function<void (std::exception_ptr, PremultipliedImage)>;
using PointForFn = std::function<ScreenCoordinate (const LatLng&)>;
using Callback = std::function<void (std::exception_ptr, PremultipliedImage, PointForFn)>;
void snapshot(ActorRef<Callback>);
private: