Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Limit Viewport #8622

Merged
merged 1 commit into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions platform/android/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Mapbox welcomes participation and contributions from everyone. If you'd like to

5.1.0 builds further on 5.0.1 and adds:

* Limit Viewport [#8622](https://github.com/mapbox/mapbox-gl-native/pull/8622)
* Transition Properties for Layer attributes [#8509](https://github.com/mapbox/mapbox-gl-native/pull/8509)
* Style wide transition duration and transition offset in milliseconds [#8576](https://github.com/mapbox/mapbox-gl-native/pull/8576)
* Transifex integration, Catalan & Dutch translations [#8556](https://github.com/mapbox/mapbox-gl-native/pull/8556)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.layers.Layer;
Expand Down Expand Up @@ -1504,6 +1505,22 @@ public boolean isAllowConcurrentMultipleOpenInfoWindows() {
return annotationManager.getInfoWindowManager().isAllowConcurrentMultipleOpenInfoWindows();
}

//
// LatLngBounds
//

/**
* Sets a LatLngBounds that constraints map transformations to this bounds.
* <p>
* Set to null to clear current bounds, newly set bounds will override previously set bounds.
* </p>
*
* @param latLngBounds the bounds to constrain the map with
*/
public void setLatLngBoundsForCameraTarget(@Nullable LatLngBounds latLngBounds) {
nativeMapView.setLatLngBounds(latLngBounds);
}

//
// Padding
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.ProjectedMeters;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException;
Expand Down Expand Up @@ -259,6 +260,13 @@ public String getStyleJson() {
return nativeGetStyleJson();
}

public void setLatLngBounds(LatLngBounds latLngBounds) {
if (isDestroyedOn("setLatLngBounds")) {
return;
}
nativeSetLatLngBounds(latLngBounds);
}

public void cancelTransitions() {
if (isDestroyedOn("cancelTransitions")) {
return;
Expand Down Expand Up @@ -999,6 +1007,8 @@ private native void nativeInitialize(NativeMapView nativeMapView,

private native String nativeGetStyleJson();

private native void nativeSetLatLngBounds(LatLngBounds latLngBounds);

private native void nativeCancelTransitions();

private native void nativeSetGestureInProgress(boolean inProgress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.InvalidMarkerPositionException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource;
Expand Down Expand Up @@ -107,6 +108,25 @@ public void onViewAction(UiController uiController, View view) {
}));
}

//
// CameraForLatLngBounds
//
@Test
public void testCameraForLatLngBounds() {
ViewUtils.checkViewIsDisplayed(R.id.mapView);
final MapboxMap mapboxMap = activity.getMapboxMap();
onView(withId(R.id.mapView)).perform(new MapboxMapAction(new InvokeViewAction() {
@Override
public void onViewAction(UiController uiController, View view) {
// set
mapboxMap.setLatLngBoundsForCameraTarget(
new LatLngBounds.Builder().include(new LatLng()).include(new LatLng(1, 1)).build());
// reset
mapboxMap.setLatLngBoundsForCameraTarget(null);
}
}));
}

//
// MinZoomLevel
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity
android:name=".activity.maplayout.LatLngBoundsForCameraActivity"
android:description="@string/description_restricted_bounds"
android:label="@string/activity_restricted_bounds">
<meta-data
android:name="@string/category"
android:value="@string/category_maplayout"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>

<!-- Storage -->
<activity
Expand All @@ -611,9 +622,11 @@
<activity
android:name=".activity.style.RuntimeStyleTestActivity"
android:screenOrientation="portrait"/>
<activity android:name=".activity.style.RuntimeStyleTimingTestActivity"
<activity
android:name=".activity.style.RuntimeStyleTimingTestActivity"
android:screenOrientation="portrait"/>
<activity android:name=".activity.espresso.EspressoTestActivity"
<activity
android:name=".activity.espresso.EspressoTestActivity"
android:screenOrientation="portrait"/>

<!-- Configuration Settings -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.mapbox.mapboxsdk.testapp.activity.maplayout;

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.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;
import com.mapbox.mapboxsdk.testapp.R;

/**
* Test Activity showcasing restricting user gestures to a bounds around Iceland.
*/
public class LatLngBoundsForCameraActivity 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);
setContentView(R.layout.activity_restricted_bounds);

mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
mapboxMap.setLatLngBoundsForCameraTarget(ICELAND_BOUNDS);
mapboxMap.setMinZoomPreference(2);
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());
boundsArea.alpha(0.25f);
boundsArea.fillColor(Color.RED);
mapboxMap.addPolygon(boundsArea);
}

private void showCrosshair() {
View crosshair = new View(this);
crosshair.setLayoutParams(new FrameLayout.LayoutParams(10, 10, Gravity.CENTER));
crosshair.setBackgroundColor(Color.BLUE);
mapView.addView(crosshair);
}

@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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="64.840048"
app:mapbox_cameraTargetLng="-18.910457"
app:mapbox_cameraZoom="4"
app:mapbox_styleUrl="@string/mapbox_style_satellite_streets"/>

</android.support.design.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<string name="activity_map_in_dialog">Dialog with map</string>
<string name="activity_marker_view_rectangle">Marker views in rectangle</string>
<string name="activity_url_transform">Url transform</string>
<string name="activity_restricted_bounds">Restrict camera to a bounds</string>

<!--Description-->
<string name="description_user_location_tracking">Tracks the location of the user</string>
Expand Down Expand Up @@ -109,6 +110,7 @@
<string name="description_marker_view_rectangle">Marker Views within a rectangle</string>
<string name="description_circle_layer">Show bus stops and route in Singapore</string>
<string name="description_url_transform">Transform urls on the fly</string>
<string name="description_restricted_bounds">Limit viewport to Iceland</string>

<!--Categories-->
<string name="category">category</string>
Expand Down
12 changes: 11 additions & 1 deletion platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "bitmap.hpp"
#include "run_loop_impl.hpp"
#include "java/util.hpp"
#include "geometry/lat_lng_bounds.hpp"

namespace mbgl {
namespace android {
Expand Down Expand Up @@ -354,6 +355,14 @@ void NativeMapView::setStyleJson(jni::JNIEnv& env, jni::String json) {
map->setStyleJSON(jni::Make<std::string>(env, json));
}

void NativeMapView::setLatLngBounds(jni::JNIEnv& env, jni::Object<mbgl::android::LatLngBounds> jBounds) {
if (jBounds) {
map->setLatLngBounds(mbgl::android::LatLngBounds::getLatLngBounds(env, jBounds));
} else {
map->setLatLngBounds(mbgl::LatLngBounds::world());
}
}

void NativeMapView::cancelTransitions(jni::JNIEnv&) {
map->cancelTransitions();
}
Expand Down Expand Up @@ -1560,7 +1569,8 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
METHOD(&NativeMapView::removeSourceById, "nativeRemoveSourceById"),
METHOD(&NativeMapView::removeSource, "nativeRemoveSource"),
METHOD(&NativeMapView::addImage, "nativeAddImage"),
METHOD(&NativeMapView::removeImage, "nativeRemoveImage")
METHOD(&NativeMapView::removeImage, "nativeRemoveImage"),
METHOD(&NativeMapView::setLatLngBounds, "nativeSetLatLngBounds")
);
}

Expand Down
3 changes: 3 additions & 0 deletions platform/android/src/native_map_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "geometry/projected_meters.hpp"
#include "style/layers/layers.hpp"
#include "style/sources/sources.hpp"
#include "geometry/lat_lng_bounds.hpp"

#include <exception>
#include <string>
Expand Down Expand Up @@ -110,6 +111,8 @@ class NativeMapView : public View, public Backend {

void setStyleJson(jni::JNIEnv&, jni::String);

void setLatLngBounds(jni::JNIEnv&, jni::Object<mbgl::android::LatLngBounds>);

void cancelTransitions(jni::JNIEnv&);

void setGestureInProgress(jni::JNIEnv&, jni::jboolean);
Expand Down