Change viewpoint

View inJavaKotlinView 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.setViewpointAsync(...) to pan to a viewpoint over the specified length of time.
    • Use MapView.setViewpointCenterAsync(...) to center the viewpoint on a Point and set a distance from the ground using a scale.
    • Use MapView.setViewpointGeometryAsync(...) to set the viewpoint to a given Geometry.

Relevant API

  • ArcGISMap
  • Geometry
  • MapView
  • Point
  • Viewpoint

Additional information

Below are some other ways to set a viewpoint:

  • setViewpoint(...)
  • setViewpointAsync(...)
  • setViewpointCenterAsync(...)
  • setViewpointGeometryAsync(...)
  • setViewpointRotationAsync(...)
  • setViewpointScaleAsync(...)

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
100
101
102
103
104
105
106
107
/*
 * Copyright 2020 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.arcgisruntime.sample.changeviewpoint

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.geometry.Point
import com.esri.arcgisruntime.geometry.PointCollection
import com.esri.arcgisruntime.geometry.Polyline
import com.esri.arcgisruntime.geometry.SpatialReferences
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.Viewpoint
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.sample.changeviewpoint.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    private val scale = 5000.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)

        // authentication with an API key or named user is required to access basemaps and other
        // location services
        ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY)

        // create a map with an imagery basemap and set it to the map view
        mapView.map = ArcGISMap(BasemapStyle.ARCGIS_IMAGERY)

        // create point for starting location
        val startPoint = Point(-14093.0, 6711377.0, SpatialReferences.getWebMercator())

        // set viewpoint of map view to starting point and scale
        mapView.setViewpointCenterAsync(startPoint, scale)
    }

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

    fun onCenterClicked(view: View) {
        // create the Waterloo location point
        val waterlooPoint = Point(-12153.0, 6710527.0, SpatialReferences.getWebMercator())
        // set the map view's viewpoint centered on Waterloo and scaled
        mapView.setViewpointCenterAsync(waterlooPoint, scale)
    }

    fun onGeometryClicked(view: View) {
        // create a collection of points around Westminster
        val westminsterPoints = PointCollection(SpatialReferences.getWebMercator())
        westminsterPoints.add(Point(-13823.0, 6710390.0))
        westminsterPoints.add(Point(-13823.0, 6710150.0))
        westminsterPoints.add(Point(-14680.0, 6710390.0))
        westminsterPoints.add(Point(-14680.0, 6710150.0))
        val geometry = Polyline(westminsterPoints)

        // set the map view's viewpoint to Westminster
        mapView.setViewpointGeometryAsync(geometry)
    }

    override fun onPause() {
        mapView.pause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
    }

    override fun onDestroy() {
        mapView.dispose()
        super.onDestroy()
    }
}

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