View on GitHub Sample viewer app

Display the map at an initial viewpoint representing a bounding geometry.

Image of Set initial viewpoint

Use case

Setting the initial viewpoint is useful when a user wishes to first load the map at a particular area of interest.

How to use the sample

When the sample loads, note the map is opened at the initial view point that is set to it.

How it works

  1. Create an ArcGISMap and specify a basemap style (for example, BasemapStyle.ArcGISImageryStandard).
  2. Define an Envelope in Web Mercator representing the bounding coordinates of the desired startup area.
  3. Construct a Viewpoint using the envelope (Viewpoint(boundingGeometry = envelope)).
  4. Assign the Viewpoint to the map using arcGISMap.initialViewpoint.
  5. Provide the ArcGISMap to the MapView composable so the MapView opens at the configured initial viewpoint.

Relevant API

  • ArcGISMap
  • Envelope
  • MapView
  • SpatialReference
  • Viewpoint

Tags

envelope, extent, initial viewpoint, zoom

Sample Code

MainActivity.kt MainActivity.kt SetInitialViewpointViewModel.kt SetInitialViewpointScreen.kt
/* Copyright 2025 Esri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.esri.arcgismaps.sample.setinitialviewpoint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
import com.esri.arcgismaps.sample.setinitialviewpoint.screens.SetInitialViewpointScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// authentication with an API key or named user is
// required to access basemaps and other location services
ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.ACCESS_TOKEN)
setContent {
SampleAppTheme {
SetInitialViewpointApp()
}
}
}
@Composable
private fun SetInitialViewpointApp() {
Surface(color = MaterialTheme.colorScheme.background) {
SetInitialViewpointScreen(
sampleName = getString(R.string.set_initial_viewpoint_app_name)
)
}
}
}