Add padding support to MapSnapshotter Android (#3882)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Bart Louwers <bart@emeel.net>
This commit is contained in:
@@ -25,7 +25,11 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
|
||||
const jni::Object<CameraPosition>& position,
|
||||
jni::jboolean _showLogo,
|
||||
jni::jboolean _showAttribution,
|
||||
const jni::String& _localIdeographFontFamily)
|
||||
const jni::String& _localIdeographFontFamily,
|
||||
jni::jfloat paddingLeft,
|
||||
jni::jfloat paddingTop,
|
||||
jni::jfloat paddingRight,
|
||||
jni::jfloat paddingBottom)
|
||||
: javaPeer(_env, _obj),
|
||||
pixelRatio(_pixelRatio) {
|
||||
// Get a reference to the JavaVM for callbacks
|
||||
@@ -57,6 +61,10 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
|
||||
}
|
||||
|
||||
if (region) {
|
||||
snapshotter->setPadding({static_cast<double>(paddingTop),
|
||||
static_cast<double>(paddingLeft),
|
||||
static_cast<double>(paddingBottom),
|
||||
static_cast<double>(paddingRight)});
|
||||
snapshotter->setRegion(LatLngBounds::getLatLngBounds(_env, region));
|
||||
}
|
||||
|
||||
@@ -138,6 +146,11 @@ void MapSnapshotter::setRegion(JNIEnv& env, const jni::Object<LatLngBounds>& reg
|
||||
snapshotter->setRegion(LatLngBounds::getLatLngBounds(env, region));
|
||||
}
|
||||
|
||||
void MapSnapshotter::setPadding(JNIEnv&, jni::jint left, jni::jint top, jni::jint right, jni::jint bottom) {
|
||||
snapshotter->setPadding(
|
||||
{static_cast<double>(top), static_cast<double>(left), static_cast<double>(bottom), static_cast<double>(right)});
|
||||
}
|
||||
|
||||
// Private methods //
|
||||
|
||||
void MapSnapshotter::activateFilesource(JNIEnv& env) {
|
||||
@@ -330,7 +343,11 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) {
|
||||
const jni::Object<CameraPosition>&,
|
||||
jni::jboolean,
|
||||
jni::jboolean,
|
||||
const jni::String&>,
|
||||
const jni::String&,
|
||||
jni::jfloat,
|
||||
jni::jfloat,
|
||||
jni::jfloat,
|
||||
jni::jfloat>,
|
||||
"nativeInitialize",
|
||||
"finalize",
|
||||
METHOD(&MapSnapshotter::setStyleUrl, "setStyleUrl"),
|
||||
@@ -345,6 +362,7 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) {
|
||||
METHOD(&MapSnapshotter::setSize, "setSize"),
|
||||
METHOD(&MapSnapshotter::setCameraPosition, "setCameraPosition"),
|
||||
METHOD(&MapSnapshotter::setRegion, "setRegion"),
|
||||
METHOD(&MapSnapshotter::setPadding, "setPadding"),
|
||||
METHOD(&MapSnapshotter::start, "nativeStart"),
|
||||
METHOD(&MapSnapshotter::cancel, "nativeCancel"));
|
||||
}
|
||||
|
||||
@@ -35,7 +35,11 @@ public:
|
||||
const jni::Object<CameraPosition>& position,
|
||||
jni::jboolean showLogo,
|
||||
jni::jboolean _showAttribution,
|
||||
const jni::String& localIdeographFontFamily);
|
||||
const jni::String& localIdeographFontFamily,
|
||||
jni::jfloat paddingLeft,
|
||||
jni::jfloat paddingTop,
|
||||
jni::jfloat paddingRight,
|
||||
jni::jfloat paddingBottom);
|
||||
|
||||
virtual ~MapSnapshotter() override;
|
||||
|
||||
@@ -49,6 +53,8 @@ public:
|
||||
|
||||
void setRegion(JNIEnv&, const jni::Object<LatLngBounds>& region);
|
||||
|
||||
void setPadding(JNIEnv&, jni::jint left, jni::jint top, jni::jint right, jni::jint bottom);
|
||||
|
||||
void start(JNIEnv&);
|
||||
|
||||
void cancel(JNIEnv&);
|
||||
|
||||
@@ -126,6 +126,10 @@ open class MapSnapshotter(context: Context, options: Options) {
|
||||
var region: LatLngBounds? = null
|
||||
private set
|
||||
|
||||
// only works when using region to specify the camera
|
||||
var regionPadding: IntArray = intArrayOf(0, 0, 0, 0)
|
||||
private set
|
||||
|
||||
/**
|
||||
* @return the camera position
|
||||
*/
|
||||
@@ -204,6 +208,16 @@ open class MapSnapshotter(context: Context, options: Options) {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* @param padding of the padding of the region.
|
||||
* This is applied before the region, if region is null, it will not work
|
||||
* @return the mutated [Options]
|
||||
*/
|
||||
fun withPadding(left:Int, top:Int, right:Int, bottom:Int): Options {
|
||||
this.regionPadding = intArrayOf(left, top, right, bottom)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pixelRatio the pixel ratio to use (default: 1)
|
||||
* @return the mutated [Options]
|
||||
@@ -349,7 +363,11 @@ open class MapSnapshotter(context: Context, options: Options) {
|
||||
options.cameraPosition,
|
||||
options.showLogo,
|
||||
options.showAttribution,
|
||||
options.localIdeographFontFamily
|
||||
options.localIdeographFontFamily,
|
||||
options.regionPadding[0] / options.pixelRatio,
|
||||
options.regionPadding[1] / options.pixelRatio,
|
||||
options.regionPadding[2] / options.pixelRatio,
|
||||
options.regionPadding[3] / options.pixelRatio
|
||||
)
|
||||
}
|
||||
|
||||
@@ -394,6 +412,14 @@ open class MapSnapshotter(context: Context, options: Options) {
|
||||
@Keep
|
||||
external fun setRegion(region: LatLngBounds?)
|
||||
|
||||
/**
|
||||
* Updates the snapshotter padding with a new [IntArray]
|
||||
*
|
||||
* @param left, top, right, bottom
|
||||
*/
|
||||
@Keep
|
||||
external fun setPadding(left:Int, top:Int, right:Int, bottom:Int)
|
||||
|
||||
/**
|
||||
* Updates the snapshotter with a new style url
|
||||
*
|
||||
@@ -746,7 +772,11 @@ open class MapSnapshotter(context: Context, options: Options) {
|
||||
position: CameraPosition?,
|
||||
showLogo: Boolean,
|
||||
showAttribution: Boolean,
|
||||
localIdeographFontFamily: String?
|
||||
localIdeographFontFamily: String?,
|
||||
paddingLeft: Float,
|
||||
paddingTop: Float,
|
||||
paddingRight: Float,
|
||||
paddingBottom: Float
|
||||
)
|
||||
|
||||
@Keep
|
||||
|
||||
@@ -60,6 +60,9 @@ public:
|
||||
void setRegion(const LatLngBounds&);
|
||||
LatLngBounds getRegion() const;
|
||||
|
||||
void setPadding(const mbgl::EdgeInsets&);
|
||||
mbgl::EdgeInsets getPadding() const;
|
||||
|
||||
style::Style& getStyle();
|
||||
const style::Style& getStyle() const;
|
||||
|
||||
|
||||
@@ -190,14 +190,26 @@ public:
|
||||
*this,
|
||||
MapOptions().withMapMode(MapMode::Static).withSize(size).withPixelRatio(pixelRatio),
|
||||
resourceOptions,
|
||||
clientOptions) {}
|
||||
clientOptions),
|
||||
region(LatLngBounds::empty()),
|
||||
regionInsets{0, 0, 0, 0} {}
|
||||
|
||||
void setRegion(const LatLngBounds& region) {
|
||||
mbgl::EdgeInsets insets{0, 0, 0, 0};
|
||||
void setRegion(const LatLngBounds& _region) {
|
||||
region = _region;
|
||||
std::vector<LatLng> latLngs = {region.southwest(), region.northeast()};
|
||||
map.jumpTo(map.cameraForLatLngs(latLngs, insets));
|
||||
map.jumpTo(map.cameraForLatLngs(latLngs, regionInsets));
|
||||
}
|
||||
|
||||
void setPadding(const mbgl::EdgeInsets& insets) {
|
||||
regionInsets = insets;
|
||||
if (!region.isEmpty()) {
|
||||
std::vector<LatLng> latLngs = {region.southwest(), region.northeast()};
|
||||
map.jumpTo(map.cameraForLatLngs(latLngs, regionInsets));
|
||||
}
|
||||
}
|
||||
|
||||
mbgl::EdgeInsets getPadding() const { return regionInsets; }
|
||||
|
||||
void snapshot(MapSnapshotter::Callback callback) {
|
||||
if (!callback) {
|
||||
Log::Error(Event::General, "MapSnapshotter::Callback is not set");
|
||||
@@ -279,6 +291,8 @@ private:
|
||||
MapSnapshotterObserver& observer;
|
||||
SnapshotterRendererFrontend frontend;
|
||||
Map map;
|
||||
LatLngBounds region;
|
||||
mbgl::EdgeInsets regionInsets;
|
||||
};
|
||||
|
||||
MapSnapshotter::MapSnapshotter(Size size,
|
||||
@@ -324,12 +338,16 @@ Size MapSnapshotter::getSize() const {
|
||||
}
|
||||
|
||||
void MapSnapshotter::setCameraOptions(const CameraOptions& options) {
|
||||
impl->getMap().jumpTo(options);
|
||||
CameraOptions optionsWithPadding = options;
|
||||
if (!options.padding) {
|
||||
// Apply region padding if no padding is explicitly set in camera options
|
||||
optionsWithPadding.withPadding(impl->getPadding());
|
||||
}
|
||||
impl->getMap().jumpTo(optionsWithPadding);
|
||||
}
|
||||
|
||||
CameraOptions MapSnapshotter::getCameraOptions() const {
|
||||
EdgeInsets insets;
|
||||
return impl->getMap().getCameraOptions(insets);
|
||||
return impl->getMap().getCameraOptions();
|
||||
}
|
||||
|
||||
void MapSnapshotter::setRegion(const LatLngBounds& region) {
|
||||
@@ -337,7 +355,15 @@ void MapSnapshotter::setRegion(const LatLngBounds& region) {
|
||||
}
|
||||
|
||||
LatLngBounds MapSnapshotter::getRegion() const {
|
||||
return impl->getMap().latLngBoundsForCamera(getCameraOptions());
|
||||
return impl->getMap().latLngBoundsForCamera(impl->getMap().getCameraOptions(getPadding()));
|
||||
}
|
||||
|
||||
void MapSnapshotter::setPadding(const mbgl::EdgeInsets& insets) {
|
||||
impl->setPadding(insets);
|
||||
}
|
||||
|
||||
mbgl::EdgeInsets MapSnapshotter::getPadding() const {
|
||||
return impl->getPadding();
|
||||
}
|
||||
|
||||
style::Style& MapSnapshotter::getStyle() {
|
||||
|
||||
Reference in New Issue
Block a user