diff options
author | tobrun <tobrun.van.nuland@gmail.com> | 2018-10-29 15:43:46 +0100 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2018-11-05 18:25:49 +0100 |
commit | 1c733568b0e14f0d2bb1b81fdf5527b300d039a4 (patch) | |
tree | c429aae4eeab7618fd985d192ed8f8e53653399e | |
parent | 68fd3a28225cccf9f1564af9dec0151f13cc8700 (diff) | |
download | qtlocation-mapboxgl-1c733568b0e14f0d2bb1b81fdf5527b300d039a4.tar.gz |
[android] - expose moveBy, replace CameraUpdateFactory hook to moveBy for scrolling api
5 files changed, 118 insertions, 22 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java index 31f13cbcff..39b937210c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdate.java @@ -20,3 +20,4 @@ public interface CameraUpdate { CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap); } + diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java index aba1b13ecd..daf5ef415d 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/camera/CameraUpdateFactory.java @@ -93,7 +93,9 @@ public final class CameraUpdateFactory { * @param xPixel Amount of pixels to scroll to in x direction * @param yPixel Amount of pixels to scroll to in y direction * @return CameraUpdate Final Camera Position + * @deprecated use {@link MapboxMap#scrollBy(float, float)} for more precise displacements when using a padded map. */ + @Deprecated public static CameraUpdate scrollBy(float xPixel, float yPixel) { return new CameraMoveUpdate(xPixel, yPixel); } @@ -336,22 +338,27 @@ public final class CameraUpdateFactory { public CameraPosition getCameraPosition(@NonNull MapboxMap mapboxMap) { UiSettings uiSettings = mapboxMap.getUiSettings(); Projection projection = mapboxMap.getProjection(); - // Calculate the new center point float viewPortWidth = uiSettings.getWidth(); float viewPortHeight = uiSettings.getHeight(); - PointF targetPoint = new PointF(viewPortWidth / 2 + x, viewPortHeight / 2 + y); + int[] padding = mapboxMap.getPadding(); - // Convert point to LatLng - LatLng latLng = projection.fromScreenLocation(targetPoint); + // we inverse the map padding, is reapplied when using moveTo/easeTo or animateTo + PointF targetPoint = new PointF( + (viewPortWidth - padding[0] + padding[1]) / 2 + x, + (viewPortHeight + padding[1] - padding[3]) / 2 + y + ); + LatLng latLng = projection.fromScreenLocation(targetPoint); CameraPosition previousPosition = mapboxMap.getCameraPosition(); - return new CameraPosition.Builder() - .target(latLng != null ? latLng : previousPosition.target) - .zoom(previousPosition.zoom) - .tilt(previousPosition.tilt) - .bearing(previousPosition.bearing) - .build(); + CameraPosition position = + new CameraPosition.Builder() + .target(latLng) + .zoom(previousPosition.zoom) + .tilt(previousPosition.tilt) + .bearing(previousPosition.bearing) + .build(); + return position; } @Override diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 7b9484d309..7172a6a52a 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -866,6 +866,29 @@ public final class MapboxMap { } } + /** + * Scrolls the camera over the map, shifting the center of view by the specified number of pixels in the x and y + * directions. + * + * @param x Amount of pixels to scroll to in x direction + * @param y Amount of pixels to scroll to in y direction + */ + public void scrollBy(float x, float y) { + nativeMapView.moveBy(x, y); + } + + /** + * Scrolls the camera over the map, shifting the center of view by the specified number of pixels in the x and y + * directions. + * + * @param x Amount of pixels to scroll to in x direction + * @param y Amount of pixels to scroll to in y direction + * @param duration Amount of time the scrolling should take + */ + public void scrollBy(float x, float y, long duration) { + nativeMapView.moveBy(x, y, duration); + } + // // Reset North // diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/TransformTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/TransformTest.kt new file mode 100644 index 0000000000..9a92a23f9b --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/TransformTest.kt @@ -0,0 +1,67 @@ +package com.mapbox.mapboxsdk.maps + +import android.support.test.espresso.UiController +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory +import com.mapbox.mapboxsdk.geometry.LatLng +import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke +import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest +import com.mapbox.mapboxsdk.testapp.activity.maplayout.SimpleMapActivity +import com.mapbox.mapboxsdk.testapp.utils.TestConstants +import org.junit.Assert.assertEquals +import org.junit.Test + + +class TransformTest: BaseActivityTest() { + + override fun getActivityClass(): Class<*> = SimpleMapActivity::class.java + + companion object { + val initialCameraUpdate = CameraUpdateFactory.newLatLngZoom(LatLng(12.0,12.0), 12.0)!! + val scrollByCameraUpdate = CameraUpdateFactory.scrollBy(400.0f,0.0f)!! + } + + @Test + fun cameraUpdateScrollByWithPadding() { + validateTestSetup() + invoke(mapboxMap) { uiController: UiController, mapboxMap: MapboxMap -> + mapboxMap.moveCamera(initialCameraUpdate) + mapboxMap.moveCamera(scrollByCameraUpdate) + val expectedCameraPosition = mapboxMap.cameraPosition + + mapboxMap.moveCamera(initialCameraUpdate) + mapboxMap.setPadding(250,250,0,0) + mapboxMap.moveCamera(scrollByCameraUpdate) + val actualCameraPosition = mapboxMap.cameraPosition + + assertEquals("Camera position latitude should match", + expectedCameraPosition.target.latitude, + actualCameraPosition.target.longitude, + TestConstants.LAT_LNG_DELTA + ) + + assertEquals("Camera position longitude should match", + expectedCameraPosition.target.longitude, + actualCameraPosition.target.longitude, + TestConstants.LAT_LNG_DELTA + ) + } + } + + @Test + fun mapboxMapScrollByWithPadding() { + validateTestSetup() + invoke(mapboxMap) { uiController: UiController, mapboxMap: MapboxMap -> + mapboxMap.moveCamera(initialCameraUpdate) + mapboxMap.scrollBy(400.0f, 0.0f) + val expectedCameraPosition = mapboxMap.cameraPosition + + mapboxMap.moveCamera(initialCameraUpdate) + mapboxMap.setPadding(250,250,0,0) + mapboxMap.scrollBy(400.0f, 0.0f) + val actualCameraPosition = mapboxMap.cameraPosition + + assertEquals("Camera position should match", expectedCameraPosition, actualCameraPosition) + } + } + +}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/ScrollByActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/ScrollByActivity.java index 4906559935..957f63329f 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/ScrollByActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/camera/ScrollByActivity.java @@ -11,7 +11,6 @@ import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.widget.SeekBar; import android.widget.TextView; - import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; @@ -36,7 +35,7 @@ public class ScrollByActivity extends AppCompatActivity implements OnMapReadyCal super.onCreate(savedInstanceState); setContentView(R.layout.activity_scroll_by); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); @@ -45,15 +44,15 @@ public class ScrollByActivity extends AppCompatActivity implements OnMapReadyCal actionBar.setDisplayShowHomeEnabled(true); } - seekBarX = (SeekBar) findViewById(R.id.seekbar_move_x); - TextView textViewX = (TextView) findViewById(R.id.textview_x); + seekBarX = findViewById(R.id.seekbar_move_x); + TextView textViewX = findViewById(R.id.textview_x); seekBarX.setOnSeekBarChangeListener(new PixelBarChangeListener(textViewX, R.string.scrollby_x_value)); - seekBarY = (SeekBar) findViewById(R.id.seekbar_move_y); - TextView textViewY = (TextView) findViewById(R.id.textview_y); + seekBarY = findViewById(R.id.seekbar_move_y); + TextView textViewY = findViewById(R.id.textview_y); seekBarY.setOnSeekBarChangeListener(new PixelBarChangeListener(textViewY, R.string.scrollby_y_value)); - mapView = (MapView) findViewById(R.id.mapView); + mapView = findViewById(R.id.mapView); mapView.setTag(true); mapView.onCreate(savedInstanceState); mapView.getMapAsync(this); @@ -65,11 +64,10 @@ public class ScrollByActivity extends AppCompatActivity implements OnMapReadyCal UiSettings uiSettings = mapboxMap.getUiSettings(); uiSettings.setLogoEnabled(false); uiSettings.setAttributionEnabled(false); - - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + FloatingActionButton fab = findViewById(R.id.fab); fab.setColorFilter(ContextCompat.getColor(ScrollByActivity.this, R.color.primary)); - fab.setOnClickListener(view -> mapboxMap.easeCamera(CameraUpdateFactory.scrollBy( - seekBarX.getProgress() * MULTIPLIER_PER_PIXEL, + fab.setOnClickListener(view -> mapboxMap.easeCamera( + CameraUpdateFactory.scrollBy(seekBarX.getProgress() * MULTIPLIER_PER_PIXEL, seekBarY.getProgress() * MULTIPLIER_PER_PIXEL) )); } @@ -133,7 +131,7 @@ public class ScrollByActivity extends AppCompatActivity implements OnMapReadyCal private int prefixTextResource; private TextView valueView; - public PixelBarChangeListener(@NonNull TextView textView, @StringRes int textRes) { + PixelBarChangeListener(@NonNull TextView textView, @StringRes int textRes) { valueView = textView; prefixTextResource = textRes; } |