From f36b998374f7374f54558b13573995d8ddb52754 Mon Sep 17 00:00:00 2001 From: tobrun Date: Wed, 26 Apr 2017 11:01:06 +0200 Subject: [PATCH 1/4] add restrict camera example --- MapboxAndroidDemo/build.gradle | 3 +- .../src/gpservices/AndroidManifest.xml | 8 ++ .../src/main/AndroidManifest.xml | 8 ++ .../mapboxandroiddemo/MainActivity.java | 7 ++ .../camera/RestrictCameraActivity.java | 106 ++++++++++++++++++ .../res/layout/activity_camera_restrict.xml | 19 ++++ .../src/main/res/values/strings.xml | 3 + 7 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java create mode 100644 MapboxAndroidDemo/src/main/res/layout/activity_camera_restrict.xml diff --git a/MapboxAndroidDemo/build.gradle b/MapboxAndroidDemo/build.gradle index 657b26571..ee6bb29d2 100644 --- a/MapboxAndroidDemo/build.gradle +++ b/MapboxAndroidDemo/build.gradle @@ -65,12 +65,11 @@ dependencies { gpservicesCompile 'com.google.firebase:firebase-crash:10.2.0' // Mapbox dependencies - compile('com.mapbox.mapboxsdk:mapbox-android-sdk:5.0.2@aar') { + compile ('com.mapbox.mapboxsdk:mapbox-android-sdk:5.1.0-SNAPSHOT@aar') { transitive = true } // Mapbox Android UI - compile 'com.mapbox.mapboxsdk:mapbox-android-ui:2.1.0' // Mapbox Navigation SDK for Android diff --git a/MapboxAndroidDemo/src/gpservices/AndroidManifest.xml b/MapboxAndroidDemo/src/gpservices/AndroidManifest.xml index c1cefd011..23d0df4bf 100644 --- a/MapboxAndroidDemo/src/gpservices/AndroidManifest.xml +++ b/MapboxAndroidDemo/src/gpservices/AndroidManifest.xml @@ -414,5 +414,13 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.mapbox.mapboxandroiddemo.MainActivity"/> + + + + diff --git a/MapboxAndroidDemo/src/main/AndroidManifest.xml b/MapboxAndroidDemo/src/main/AndroidManifest.xml index 87db05cb4..90ad5ffb2 100644 --- a/MapboxAndroidDemo/src/main/AndroidManifest.xml +++ b/MapboxAndroidDemo/src/main/AndroidManifest.xml @@ -387,6 +387,14 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.mapbox.mapboxandroiddemo.MainActivity"/> + + + + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java index ef66afb33..51f09ff51 100644 --- a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java @@ -32,6 +32,7 @@ import com.mapbox.mapboxandroiddemo.examples.basics.SupportMapFragmentActivity; import com.mapbox.mapboxandroiddemo.examples.camera.AnimateMapCameraActivity; import com.mapbox.mapboxandroiddemo.examples.camera.BoundingBoxCameraActivity; +import com.mapbox.mapboxandroiddemo.examples.camera.RestrictCameraActivity; import com.mapbox.mapboxandroiddemo.examples.dds.ChoroplethZoomChangeActivity; import com.mapbox.mapboxandroiddemo.examples.dds.StyleCirclesCategoricallyActivity; import com.mapbox.mapboxandroiddemo.examples.dds.StyleLineIdentityPropertyActivity; @@ -325,6 +326,12 @@ private void listItems(int id) { new Intent(MainActivity.this, BoundingBoxCameraActivity.class), R.string.activity_camera_bounding_box_url )); + exampleItemModel.add(new ExampleItemModel( + R.string.activity_camera_restrict_title, + R.string.activity_camera_restrict_description, + new Intent(MainActivity.this, RestrictCameraActivity.class), + R.string.activity_camera_restrict_url + )); currentCategory = R.id.nav_camera; break; case R.id.nav_offline: diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java new file mode 100644 index 000000000..39d54b3f9 --- /dev/null +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java @@ -0,0 +1,106 @@ +package com.mapbox.mapboxandroiddemo.examples.camera; + +import android.graphics.Color; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; + +import com.mapbox.mapboxandroiddemo.R; +import com.mapbox.mapboxsdk.Mapbox; +import com.mapbox.mapboxsdk.annotations.PolygonOptions; +import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.geometry.LatLngBounds; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; + +public class RestrictCameraActivity extends AppCompatActivity implements OnMapReadyCallback { + + private static final LatLngBounds ICELAND_BOUNDS = new LatLngBounds.Builder() + .include(new LatLng(66.852863, -25.985652)) + .include(new LatLng(62.985661, -12.626277)) + .build(); + + private MapView mapView; + private MapboxMap mapboxMap; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Mapbox access token is configured here. This needs to be called either in your application + // object or in the same activity which contains the mapview. + Mapbox.getInstance(this, getString(R.string.access_token)); + + // This contains the MapView in XML and needs to be called after the account manager + setContentView(R.layout.activity_camera_restrict); + + mapView = (MapView) findViewById(R.id.mapView); + mapView.onCreate(savedInstanceState); + mapView.getMapAsync(this); + } + + @Override + public void onMapReady(MapboxMap mapboxMap) { + this.mapboxMap = mapboxMap; + + // Set bounds to Iceland + mapboxMap.setLatLngBoundsForCameraTarget(ICELAND_BOUNDS); + mapboxMap.setMinZoomPreference(2); + + // Visualise bounds area + showBoundsArea(); + } + + private void showBoundsArea() { + PolygonOptions boundsArea = new PolygonOptions() + .add(ICELAND_BOUNDS.getNorthWest()) + .add(ICELAND_BOUNDS.getNorthEast()) + .add(ICELAND_BOUNDS.getSouthEast()) + .add(ICELAND_BOUNDS.getSouthWest()); + boundsArea.alpha(0.25f); + boundsArea.fillColor(Color.RED); + mapboxMap.addPolygon(boundsArea); + } + + @Override + protected void onStart() { + super.onStart(); + mapView.onStart(); + } + + @Override + protected void onResume() { + super.onResume(); + mapView.onResume(); + } + + @Override + protected void onPause() { + super.onPause(); + mapView.onPause(); + } + + @Override + protected void onStop() { + super.onStop(); + mapView.onStop(); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + mapView.onSaveInstanceState(outState); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + mapView.onDestroy(); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + mapView.onLowMemory(); + } +} \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_camera_restrict.xml b/MapboxAndroidDemo/src/main/res/layout/activity_camera_restrict.xml new file mode 100644 index 000000000..f63f21912 --- /dev/null +++ b/MapboxAndroidDemo/src/main/res/layout/activity_camera_restrict.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/values/strings.xml b/MapboxAndroidDemo/src/main/res/values/strings.xml index e868548f6..cfb083774 100644 --- a/MapboxAndroidDemo/src/main/res/values/strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/strings.xml @@ -73,6 +73,7 @@ Animate marker position Animate the map camera Fit camera in bounding box + Restrict map panning A simple offline map Offline manager Query a map feature @@ -126,6 +127,7 @@ Animate the marker to a new position on the map. "Animate the map's camera position, tilt, bearing, and zoom." Position the camera so that all the given markers are in view. + Prevent a map from being panned to a different place. Download and view an offline map using the Mapbox Android SDK. Download, view, navigate to, and delete an offline region. Click the map to add a marker at the location and display the maps property information for that feature. @@ -181,6 +183,7 @@ http://i.imgur.com/XegvIKr.png https://www.mapbox.com/android-sdk/images/animate-camera.gif http://i.imgur.com/A0JL21Q.png + http://i.imgur.com/DSXAQQl.png https://www.mapbox.com/android-sdk/images/offline-map.png http://i.imgur.com/tDlcNIg.png http://i.imgur.com/bImud7u.png From e1f5fc98e6e5f8beee5a0f5cc5914286f798d0be Mon Sep 17 00:00:00 2001 From: Langston Date: Thu, 27 Apr 2017 18:16:52 -0700 Subject: [PATCH 2/4] Adjusted bounds and added crosshair method to example's activity --- .../camera/RestrictCameraActivity.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java index 39d54b3f9..5765ed67c 100644 --- a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java @@ -3,10 +3,16 @@ import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.view.Gravity; +import android.view.View; +import android.widget.FrameLayout; import com.mapbox.mapboxandroiddemo.R; import com.mapbox.mapboxsdk.Mapbox; +import com.mapbox.mapboxsdk.annotations.Marker; +import com.mapbox.mapboxsdk.annotations.MarkerViewOptions; import com.mapbox.mapboxsdk.annotations.PolygonOptions; +import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.maps.MapView; @@ -15,13 +21,14 @@ public class RestrictCameraActivity extends AppCompatActivity implements OnMapReadyCallback { - private static final LatLngBounds ICELAND_BOUNDS = new LatLngBounds.Builder() - .include(new LatLng(66.852863, -25.985652)) - .include(new LatLng(62.985661, -12.626277)) + private static final LatLngBounds AUSTRALIA_BOUNDS = new LatLngBounds.Builder() + .include(new LatLng(-9.136343, 109.372126)) + .include(new LatLng(-44.640488, 158.590484)) .build(); private MapView mapView; private MapboxMap mapboxMap; + private Marker marker; @Override protected void onCreate(Bundle savedInstanceState) { @@ -44,24 +51,35 @@ public void onMapReady(MapboxMap mapboxMap) { this.mapboxMap = mapboxMap; // Set bounds to Iceland - mapboxMap.setLatLngBoundsForCameraTarget(ICELAND_BOUNDS); + mapboxMap.setLatLngBoundsForCameraTarget(AUSTRALIA_BOUNDS); mapboxMap.setMinZoomPreference(2); // Visualise bounds area showBoundsArea(); + + showCrosshair(); + } private void showBoundsArea() { PolygonOptions boundsArea = new PolygonOptions() - .add(ICELAND_BOUNDS.getNorthWest()) - .add(ICELAND_BOUNDS.getNorthEast()) - .add(ICELAND_BOUNDS.getSouthEast()) - .add(ICELAND_BOUNDS.getSouthWest()); + .add(AUSTRALIA_BOUNDS.getNorthWest()) + .add(AUSTRALIA_BOUNDS.getNorthEast()) + .add(AUSTRALIA_BOUNDS.getSouthEast()) + .add(AUSTRALIA_BOUNDS.getSouthWest()); boundsArea.alpha(0.25f); boundsArea.fillColor(Color.RED); mapboxMap.addPolygon(boundsArea); } + private void showCrosshair() { + View crosshair = new View(this); + crosshair.setLayoutParams(new FrameLayout.LayoutParams(15, 15, Gravity.CENTER)); + crosshair.setBackgroundColor(Color.GREEN); + mapView.addView(crosshair); + } + + @Override protected void onStart() { super.onStart(); From 01eddef1e26df97213ce572e1660fd68cdad43fd Mon Sep 17 00:00:00 2001 From: Langston Date: Thu, 27 Apr 2017 18:17:08 -0700 Subject: [PATCH 3/4] Adjusted map target and zoom in XML layout --- .../src/main/res/layout/activity_camera_restrict.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_camera_restrict.xml b/MapboxAndroidDemo/src/main/res/layout/activity_camera_restrict.xml index f63f21912..a2a66a44f 100644 --- a/MapboxAndroidDemo/src/main/res/layout/activity_camera_restrict.xml +++ b/MapboxAndroidDemo/src/main/res/layout/activity_camera_restrict.xml @@ -11,9 +11,9 @@ android:id="@id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" - mapbox:mapbox_cameraTargetLat="64.840048" - mapbox:mapbox_cameraTargetLng="-18.910457" - mapbox:mapbox_cameraZoom="4" + mapbox:mapbox_cameraTargetLat="-26.145" + mapbox:mapbox_cameraTargetLng="134.312" + mapbox:mapbox_cameraZoom="2" mapbox:mapbox_styleUrl="@string/mapbox_style_satellite_streets"/> \ No newline at end of file From ce9feea20715e4a253cdedb27abe0d122400985c Mon Sep 17 00:00:00 2001 From: Langston Date: Thu, 27 Apr 2017 18:27:23 -0700 Subject: [PATCH 4/4] Checkstyle adjustment: removing unused imports --- .../examples/camera/RestrictCameraActivity.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java index 5765ed67c..e71996fb5 100644 --- a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/camera/RestrictCameraActivity.java @@ -10,9 +10,7 @@ import com.mapbox.mapboxandroiddemo.R; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.annotations.Marker; -import com.mapbox.mapboxsdk.annotations.MarkerViewOptions; import com.mapbox.mapboxsdk.annotations.PolygonOptions; -import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.maps.MapView;