diff --git a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp index 051fc359d..6f38135a5 100644 --- a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp +++ b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.cpp @@ -25,7 +25,11 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, const jni::Object& 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(paddingTop), + static_cast(paddingLeft), + static_cast(paddingBottom), + static_cast(paddingRight)}); snapshotter->setRegion(LatLngBounds::getLatLngBounds(_env, region)); } @@ -138,6 +146,11 @@ void MapSnapshotter::setRegion(JNIEnv& env, const jni::Object& 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(top), static_cast(left), static_cast(bottom), static_cast(right)}); +} + // Private methods // void MapSnapshotter::activateFilesource(JNIEnv& env) { @@ -330,7 +343,11 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) { const jni::Object&, 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")); } diff --git a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp index af69dbeed..b5d5d0e1d 100644 --- a/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp +++ b/platform/android/MapLibreAndroid/src/cpp/snapshotter/map_snapshotter.hpp @@ -35,7 +35,11 @@ public: const jni::Object& 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& region); + void setPadding(JNIEnv&, jni::jint left, jni::jint top, jni::jint right, jni::jint bottom); + void start(JNIEnv&); void cancel(JNIEnv&); diff --git a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt index c498b9402..41b3805db 100644 --- a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt +++ b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/snapshotter/MapSnapshotter.kt @@ -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 diff --git a/platform/default/include/mbgl/map/map_snapshotter.hpp b/platform/default/include/mbgl/map/map_snapshotter.hpp index c17ae3815..5bcbf98ed 100644 --- a/platform/default/include/mbgl/map/map_snapshotter.hpp +++ b/platform/default/include/mbgl/map/map_snapshotter.hpp @@ -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; diff --git a/platform/default/src/mbgl/map/map_snapshotter.cpp b/platform/default/src/mbgl/map/map_snapshotter.cpp index fa74b076a..eb81c132f 100644 --- a/platform/default/src/mbgl/map/map_snapshotter.cpp +++ b/platform/default/src/mbgl/map/map_snapshotter.cpp @@ -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 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 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() {