Change viewpoint

View on GitHubSample viewer app

Set the map view to a new viewpoint.

Image of change viewpoint

Use case

Programmatically navigate to a specified location in the map or scene. Use this to focus on a particular point or area of interest.

How to use the sample

Select a viewpoint option to see the map view move to that location.

How it works

  1. Create a new ArcGISMap object and set it to the MapView object.
  2. Change the map's Viewpoint using one of the available methods:
    • Use MapView.setViewpointAnimated(...) to pan to a viewpoint over the specified length of time.
    • Use MapView.setViewpointCenter(...) to center the viewpoint on a Point and set a distance from the ground using a scale.
    • Use MapView.setViewpointGeometry(...) to set the viewpoint to a given Geometry.

Relevant API

  • ArcGISMap
  • Geometry
  • MapView
  • Point
  • Viewpoint

Additional information

Below are multiple ways to set a viewpoint:

mapView.setViewpoint(...)
mapView.setViewpointCenter(...)
mapView.setViewpointGeometry(...)
mapView.setViewpointRotation(...)
mapView.setViewpointScale(...)
mapView.setViewpointAnimated(...)

Tags

animate, extent, pan, rotate, scale, view, zoom

Sample Code

MainActivity.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Copyright 2022 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.changeviewpoint

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.PolylineBuilder
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.esri.arcgismaps.sample.changeviewpoint.databinding.ActivityMainBinding
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private val viewpointScale = 5000.0

    // set up data binding for the activity
    private val activityMainBinding: ActivityMainBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_main)
    }

    private val mapView by lazy {
        activityMainBinding.mapView
    }

    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.API_KEY)
        // add the MapView to the lifecycle
        lifecycle.addObserver(mapView)
        // create and add a map with a imagery basemap style
        mapView.map = ArcGISMap(BasemapStyle.ArcGISImagery)
        // set the start point of the ViewPoint
        val startPoint = Point(-14093.0, 6711377.0, SpatialReference.webMercator())
        lifecycleScope.launch {
            // set viewpoint of map view to starting point and scale
            mapView.setViewpointCenter(startPoint, viewpointScale)
        }
    }

    fun onGeometryClicked(view: View) {
        // create a collection of points around Westminster
        val westminsterPolylineBuilder = PolylineBuilder(SpatialReference.webMercator()) {
            addPoint(Point(-13823.0, 6710390.0))
            addPoint(Point(-13823.0, 6710150.0))
            addPoint(Point(-14680.0, 6710390.0))
            addPoint(Point(-14680.0, 6710150.0))
        }
        val geometry = westminsterPolylineBuilder.toGeometry()
        // set the map view's viewpoint to Westminster
        lifecycleScope.launch {
            mapView.setViewpointGeometry(geometry)
        }
    }

    fun onCenterClicked(view: View) {
        // create the Waterloo location point
        val waterlooPoint = Point(-12153.0, 6710527.0, SpatialReference.webMercator())
        // set the map view's viewpoint centered on Waterloo and scaled
        lifecycleScope.launch {
            mapView.setViewpointCenter(waterlooPoint, viewpointScale)
        }
    }

    fun onAnimateClicked(view: View) {
        // create the London location point
        val londonPoint = Point(-14093.0, 6711377.0, SpatialReference.webMercator())
        // create the viewpoint with the London point and scale
        val viewpoint = Viewpoint(londonPoint, viewpointScale)
        // set the map view's viewpoint to London with a seven second animation duration
        lifecycleScope.launch {
            mapView.setViewpointAnimated(viewpoint, 7f)
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.