Restrict map panning
Restrict the map's viewport to a specified set of coordinates for gesture-based camera changes. The viewport can still be changed programmatically.
<?xml version="1.0" encoding="utf-8"?><androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:mapbox="http://schemas.android.com/apk/res-auto"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"> <com.mapbox.maps.MapViewandroid:id="@id/mapView"android:layout_width="match_parent"android:layout_height="match_parent"mapbox:mapbox_cameraTargetLat="37.742646"mapbox:mapbox_cameraTargetLng=" -122.433247"mapbox:mapbox_cameraZoom="12"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>
package com.mapbox.maps.testapp.examples.camera import android.annotation.SuppressLintimport android.graphics.Colorimport android.os.Bundleimport android.view.Gravityimport android.view.Menuimport android.view.MenuItemimport android.view.Viewimport android.widget.FrameLayoutimport androidx.appcompat.app.AppCompatActivityimport com.mapbox.geojson.FeatureCollectionimport com.mapbox.geojson.Pointimport com.mapbox.geojson.Polygonimport com.mapbox.maps.CameraBoundsOptionsimport com.mapbox.maps.CoordinateBoundsimport com.mapbox.maps.MapboxMapimport com.mapbox.maps.Styleimport com.mapbox.maps.extension.style.sources.generated.GeoJsonSourceimport com.mapbox.maps.extension.style.sources.generated.geoJsonSourceimport com.mapbox.maps.extension.style.sources.getSourceimport com.mapbox.maps.extension.style.styleimport com.mapbox.maps.testapp.Rimport com.mapbox.maps.testapp.databinding.ActivityRestrictBoundsBinding /*** Test activity showcasing restricting user gestures to a bounds around Iceland, almost worldview and IDL.*/class RestrictBoundsActivity : AppCompatActivity() { private lateinit var mapboxMap: MapboxMapprivate lateinit var binding: ActivityRestrictBoundsBinding override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ActivityRestrictBoundsBinding.inflate(layoutInflater)setContentView(binding.root)mapboxMap = binding.mapView.mapboxMapmapboxMap.loadStyle(style(Style.STANDARD) {+geoJsonSource(BOUNDS_ID) {featureCollection(FeatureCollection.fromFeatures(listOf()))}}) { setupBounds(SAN_FRANCISCO_BOUND) }showCrosshair()} override fun onCreateOptionsMenu(menu: Menu): Boolean {menuInflater.inflate(R.menu.menu_bounds, menu)return true} override fun onOptionsItemSelected(item: MenuItem): Boolean {return when (item.itemId) {R.id.menu_action_san_francisco_bounds -> {setupBounds(SAN_FRANCISCO_BOUND)true}R.id.menu_action_allmost_world_bounds -> {setupBounds(ALMOST_WORLD_BOUNDS)true}R.id.menu_action_cross_idl -> {setupBounds(CROSS_IDL_BOUNDS)true}R.id.menu_action_reset -> {setupBounds(INFINITE_BOUNDS)true}else -> {super.onOptionsItemSelected(item)}}} private fun setupBounds(bounds: CameraBoundsOptions) {mapboxMap.setBounds(bounds)showBoundsArea(bounds)} private fun showBoundsArea(boundsOptions: CameraBoundsOptions) {val source = mapboxMap.style!!.getSource(BOUNDS_ID) as GeoJsonSourceval bounds = boundsOptions.boundsval list = mutableListOf<List<Point>>()bounds?.let {if (!it.infiniteBounds) {val northEast = it.northeastval southWest = it.southwestval northWest = Point.fromLngLat(southWest.longitude(), northEast.latitude())val southEast = Point.fromLngLat(northEast.longitude(), southWest.latitude())list.add(mutableListOf(northEast, southEast, southWest, northWest, northEast))}}if (list.isNotEmpty()) {source.geometry(Polygon.fromLngLats(list))}} private fun showCrosshair() {val crosshair = View(this)crosshair.layoutParams = FrameLayout.LayoutParams(10, 10, Gravity.CENTER)crosshair.setBackgroundColor(Color.BLUE)binding.mapView.addView(crosshair)} companion object {private const val BOUNDS_ID = "BOUNDS_ID"private val SAN_FRANCISCO_BOUND: CameraBoundsOptions = CameraBoundsOptions.Builder().bounds(CoordinateBounds(Point.fromLngLat(-122.66336, 37.492987),Point.fromLngLat(-122.250481, 37.87165),false)).minZoom(10.0).build() private val ALMOST_WORLD_BOUNDS: CameraBoundsOptions = CameraBoundsOptions.Builder().bounds(CoordinateBounds(Point.fromLngLat(-170.0, -20.0),Point.fromLngLat(170.0, 20.0),false)).minZoom(2.0).build() @SuppressLint("Range")private val CROSS_IDL_BOUNDS: CameraBoundsOptions = CameraBoundsOptions.Builder().bounds(CoordinateBounds(Point.fromLngLat(170.0202020, -20.0),Point.fromLngLat(190.0, 20.0),false)).minZoom(2.0).build() private val INFINITE_BOUNDS: CameraBoundsOptions = CameraBoundsOptions.Builder().bounds(CoordinateBounds(Point.fromLngLat(0.0, 0.0),Point.fromLngLat(0.0, 0.0),true)).build()}}
Was this example helpful?