diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-10-02 17:22:56 +0300 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-10-31 19:52:08 +0200 |
commit | 7481d317e772879fe4f346b3e0f11679ee26e7b1 (patch) | |
tree | c5f7adcdac40994da90f7dc9efd1530317528249 | |
parent | d37144d2f8be08f499918232ec49a3cae801de46 (diff) | |
download | qtlocation-mapboxgl-7481d317e772879fe4f346b3e0f11679ee26e7b1.tar.gz |
[android] map snapshotter - add mutators
9 files changed, 210 insertions, 4 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java index 72df86d80d..8118f9ad92 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java @@ -184,7 +184,7 @@ public class MapSnapshotter { * Starts loading and rendering the snapshot. The callbacks will be fired * on the calling thread. * - * @param callback the callback to use when the snapshot is ready + * @param callback the callback to use when the snapshot is ready * @param errorHandler the error handler to use on snapshot errors */ public void start(@NonNull MapboxMap.SnapshotReadyCallback callback, ErrorHandler errorHandler) { @@ -198,11 +198,41 @@ public class MapSnapshotter { } /** + * Updates the snapshotter with a new size + * + * @param width the width + * @param height the height + */ + public native void setSize(int width, int height); + + /** + * Updates the snapshotter with a new {@link CameraPosition} + * + * @param cameraPosition the camera position + */ + public native void setCameraPosition(CameraPosition cameraPosition); + + /** + * Updates the snapshotter with a new {@link LatLngBounds} + * + * @param region the region + */ + public native void setRegion(LatLngBounds region); + + /** + * Updates the snapshotter with a new style url + * + * @param styleUrl the style url + */ + public native void setStyleUrl(String styleUrl); + + + /** * Must be called in on the thread * the object was created on. */ public void cancel() { - callback = null; + reset(); nativeCancel(); } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index bf97749b9e..d6237bc161 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -377,6 +377,16 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".activity.FeatureOverviewActivity"/> </activity> + <activity android:name=".activity.snapshot.MapSnapshotterReuseActivity" + android:description="@string/description_map_snapshotter_reuse" + android:label="@string/activity_map_snapshotter_reuse"> + <meta-data + android:name="@string/category" + android:value="@string/category_imagegenerator"/> + <meta-data + android:name="android.support.PARENT_ACTIVITY" + android:value=".activity.FeatureOverviewActivity"/> + </activity> <activity android:name=".activity.maplayout.DoubleMapActivity" android:description="@string/description_doublemap" diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterReuseActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterReuseActivity.java new file mode 100644 index 0000000000..ac51bd9d5f --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/snapshot/MapSnapshotterReuseActivity.java @@ -0,0 +1,110 @@ +package com.mapbox.mapboxsdk.testapp.activity.snapshot; + +import android.graphics.Bitmap; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.ImageView; + +import com.mapbox.mapboxsdk.camera.CameraPosition; +import com.mapbox.mapboxsdk.constants.Style; +import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.geometry.LatLngBounds; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter; +import com.mapbox.mapboxsdk.testapp.R; + +import java.util.Random; + +/** + * Test activity showing how to use a the {@link MapSnapshotter} + */ +public class MapSnapshotterReuseActivity extends AppCompatActivity implements MapboxMap.SnapshotReadyCallback { + + private MapSnapshotter mapSnapshotter; + private View fab; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_map_snapshotter_reuse); + + fab = findViewById(R.id.fab); + fab.setVisibility(View.INVISIBLE); + fab.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + fab.setVisibility(View.INVISIBLE); + + mapSnapshotter.setStyleUrl(getRandomStyle()); + if (random.nextInt(2) == 0) { + mapSnapshotter.setCameraPosition(getRandomCameraPosition()); + } else { + mapSnapshotter.setRegion(getRandomBounds()); + } + if (random.nextInt(2) == 0) { + mapSnapshotter.setSize(512, 512); + } else { + mapSnapshotter.setSize(256, 256); + } + mapSnapshotter.start(MapSnapshotterReuseActivity.this); + } + }); + + mapSnapshotter = new MapSnapshotter( + getApplicationContext(), + new MapSnapshotter.Options(512, 512) + ); + + mapSnapshotter.start(MapSnapshotterReuseActivity.this); + } + + @Override + public void onSnapshotReady(Bitmap snapshot) { + fab.setVisibility(View.VISIBLE); + ImageView imageView = (ImageView) findViewById(R.id.snapshot_image); + imageView.setImageBitmap(snapshot); + } + + private LatLngBounds getRandomBounds() { + return LatLngBounds.from( + randomInRange(-5, 5), + randomInRange(-5, 5), + randomInRange(5, 10), + randomInRange(5, 10) + ); + } + + private CameraPosition getRandomCameraPosition() { + return new CameraPosition.Builder() + .target(new LatLng(randomInRange(-80, 80), randomInRange(-160, 160))) + .zoom(randomInRange(2, 10)) + .bearing(randomInRange(0, 90)) + .build(); + } + + public String getRandomStyle() { + switch (random.nextInt(5)) { + case 0: + return Style.DARK; + case 1: + return Style.LIGHT; + case 2: + return Style.MAPBOX_STREETS; + case 3: + return Style.OUTDOORS; + case 4: + return Style.SATELLITE_STREETS; + default: + return Style.TRAFFIC_DAY; + } + } + + private static Random random = new Random(); + + public static float randomInRange(float min, float max) { + return (random.nextFloat() * (max - min)) + min; + } + +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_map_snapshotter_reuse.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_map_snapshotter_reuse.xml new file mode 100644 index 0000000000..5ce25a69d0 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_map_snapshotter_reuse.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <ImageView + android:id="@+id/snapshot_image" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_centerInParent="true" + android:contentDescription=""/> + + <android.support.design.widget.FloatingActionButton + android:id="@+id/fab" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignParentBottom="true" + android:layout_alignParentEnd="true" + android:layout_alignParentRight="true" + android:layout_gravity="end|bottom" + android:layout_margin="@dimen/fab_margin" + android:src="@drawable/ic_animate_coordinates"/> + +</RelativeLayout> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml index 4d1f7eac38..94dc669781 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml @@ -60,6 +60,7 @@ <string name="description_animated_image_source">Shows how to animate georeferenced images</string> <string name="description_bottom_sheet">Show 2 MapView on screen with a bottom sheet</string> <string name="description_map_snapshotter">Show a static bitmap taken with the MapSnapshotter</string> + <string name="description_map_snapshotter_reuse">Show how to reuse a MapSnapshotter instance</string> <string name="description_camera_animator">Use Android SDK Animators to animate camera position changes</string> <string name="description_symbol_generator">Use Android SDK Views as symbols</string> </resources>
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml index 8f394d0eb4..4942bcab36 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml @@ -60,6 +60,7 @@ <string name="activity_animated_image_source">Animated Image Source</string> <string name="activity_bottom_sheet">Bottom sheet</string> <string name="activity_map_snapshotter">Map Snapshotter</string> + <string name="activity_map_snapshotter_reuse">Map Snapshotter Reuse</string> <string name="activity_camera_animator">Animator animation</string> <string name="activity_symbol_generator">SymbolGenerator</string> </resources>
\ No newline at end of file diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index d64218d11a..74e43c70a1 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -81,9 +81,26 @@ void MapSnapshotter::start(JNIEnv&) { void MapSnapshotter::cancel(JNIEnv&) { MBGL_VERIFY_THREAD(tid); - snapshotCallback.reset(); - snapshotter.reset(); +} + + +void MapSnapshotter::setStyleUrl(JNIEnv& env, jni::String styleURL) { + snapshotter->setStyleURL(jni::Make<std::string>(env, styleURL)); +} + +void MapSnapshotter::setSize(JNIEnv&, jni::jint width, jni::jint height) { + auto size = mbgl::Size { static_cast<uint32_t>(width), static_cast<uint32_t>(height) }; + snapshotter->setSize(size); +} + +void MapSnapshotter::setCameraPosition(JNIEnv& env, jni::Object<CameraPosition> position) { + auto options = CameraPosition::getCameraOptions(env, position); + snapshotter->setCameraOptions(options); +} + +void MapSnapshotter::setRegion(JNIEnv& env, jni::Object<LatLngBounds> region) { + snapshotter->setRegion(LatLngBounds::getLatLngBounds(env, region)); } // Static methods // @@ -101,6 +118,10 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) { std::make_unique<MapSnapshotter, JNIEnv&, jni::Object<MapSnapshotter>, jni::Object<FileSource>, jni::jfloat, jni::jint, jni::jint, jni::String, jni::Object<LatLngBounds>, jni::Object<CameraPosition>, jni::String>, "nativeInitialize", "finalize", + METHOD(&MapSnapshotter::setStyleUrl, "setStyleUrl"), + METHOD(&MapSnapshotter::setSize, "setSize"), + METHOD(&MapSnapshotter::setCameraPosition, "setCameraPosition"), + METHOD(&MapSnapshotter::setRegion, "setRegion"), METHOD(&MapSnapshotter::start, "nativeStart"), METHOD(&MapSnapshotter::cancel, "nativeCancel") ); diff --git a/platform/android/src/snapshotter/map_snapshotter.hpp b/platform/android/src/snapshotter/map_snapshotter.hpp index 093f589c05..fa8a2d7a5a 100644 --- a/platform/android/src/snapshotter/map_snapshotter.hpp +++ b/platform/android/src/snapshotter/map_snapshotter.hpp @@ -40,6 +40,14 @@ public: ~MapSnapshotter(); + void setStyleUrl(JNIEnv&, jni::String styleURL); + + void setSize(JNIEnv&, jni::jint width, jni::jint height); + + void setCameraPosition(JNIEnv&, jni::Object<CameraPosition> position); + + void setRegion(JNIEnv&, jni::Object<LatLngBounds> region); + void start(JNIEnv&); void cancel(JNIEnv&); diff --git a/platform/default/mbgl/map/map_snapshotter.cpp b/platform/default/mbgl/map/map_snapshotter.cpp index 058e03c71b..e542e2fb54 100644 --- a/platform/default/mbgl/map/map_snapshotter.cpp +++ b/platform/default/mbgl/map/map_snapshotter.cpp @@ -76,6 +76,7 @@ std::string MapSnapshotter::Impl::getStyleURL() const { void MapSnapshotter::Impl::setSize(Size size) { map.setSize(size); + frontend.setSize(size); } Size MapSnapshotter::Impl::getSize() const { |