Change viewpoint

View on GitHub

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

The map view has several methods for setting its current viewpoint. Select a viewpoint from the UI to see the viewpoint changed using that method.

How it works

  1. Create a new ArcGISMapView with an ArcGISMapViewController, and assign an ArcGISMap to the ArcGISMapViewController.arcGISMap property.
  2. Change the map's Viewpoint using one of the available methods:
  • Use ArcGISMapViewController.setViewpointGeometry() to set the viewpoint to a given Geometry.
  • Use ArcGISMapViewController.setViewpointCenter() to center the viewpoint on a ArcGISPoint and set a distance from the ground using a scale.
  • Use ArcGISMapViewController.setViewpointAnimated() to pan to a viewpoint over the specified length of time.

Relevant API

  • ArcGISMap
  • ArcGISMapView
  • ArcGISPoint
  • Geometry
  • Viewpoint

Additional information

Below are some other ways to set a viewpoint:

  • ArcGISMapViewController.setViewpoint
  • ArcGISMapViewController.setViewpointRotation
  • ArcGISMapViewController.setViewpointScale
  • ArcGISMapViewController.setViewpointWithDurationAndCurve

Tags

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

Sample Code

change_viewpoint.dart
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2024 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
//
//   https://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.
//

import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:flutter/material.dart';
import '../../utils/sample_state_support.dart';

class ChangeViewpoint extends StatefulWidget {
  const ChangeViewpoint({super.key});

  @override
  State<ChangeViewpoint> createState() => _ChangeViewpointState();
}

class _ChangeViewpointState extends State<ChangeViewpoint>
    with SampleStateSupport {
  // Create a controller for the map view.
  final _mapViewController = ArcGISMapView.createController();

  // Define the Redlands polygon.
  late PolygonBuilder _redlandsEnvelope;

  // Define the Edinburgh polygon.
  late PolygonBuilder _edinburghEnvelope;

  // String array to store titles for the viewpoints specified above.
  final _viewpointTitles = [
    'Geometry',
    'Center & Scale',
    'Animate',
  ];

  // Create variable for holding state relating to the viewpoint.
  String? _selectedViewpoint;

  // Define an envelope that can navigate to the full extent.
  Envelope? _fullExtent;

  // A flag for when the map view is ready and controls can be used.
  var _ready = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: false,
        child: Stack(
          children: [
            Column(
              children: [
                Expanded(
                  // Add a map view to the widget tree and set a controller.
                  child: ArcGISMapView(
                    controllerProvider: () => _mapViewController,
                    onMapViewReady: onMapViewReady,
                  ),
                ),
                // Build the bottom menu.
                buildBottomMenu(),
              ],
            ),
            // Display a progress indicator and prevent interaction until state is ready.
            Visibility(
              visible: !_ready,
              child: const SizedBox.expand(
                child: ColoredBox(
                  color: Colors.white30,
                  child: Center(child: CircularProgressIndicator()),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void onMapViewReady() async {
    // Create new Map with basemap and initial location.
    final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
    // Assign the map to the ArcGISMapView.
    _mapViewController.arcGISMap = map;

    // Load the map so that we get the full extent.
    await map.load();
    _fullExtent = map.basemap!.baseLayers.first.fullExtent;

    // Coordinates for Redlands.
    _redlandsEnvelope =
        PolygonBuilder(spatialReference: SpatialReference.webMercator);
    _redlandsEnvelope.addPointXY(x: -13049785.1566222, y: 4032064.6003424);
    _redlandsEnvelope.addPointXY(x: -13049785.1566222, y: 4040202.42595729);
    _redlandsEnvelope.addPointXY(x: -13037033.5780234, y: 4032064.6003424);
    _redlandsEnvelope.addPointXY(x: -13037033.5780234, y: 4040202.42595729);

    // Coordinates for Edinburgh.
    _edinburghEnvelope =
        PolygonBuilder(spatialReference: SpatialReference.webMercator);
    _edinburghEnvelope.addPointXY(x: -354262.156621384, y: 7548092.94093301);
    _edinburghEnvelope.addPointXY(x: -354262.156621384, y: 7548901.50684376);
    _edinburghEnvelope.addPointXY(x: -353039.164455303, y: 7548092.94093301);
    _edinburghEnvelope.addPointXY(x: -353039.164455303, y: 7548901.50684376);

    // Set the ready state variable to true to enable the sample UI.
    setState(() => _ready = true);
  }

  Widget buildBottomMenu() {
    return Center(
      // A drop down button for selecting viewpoint.
      child: DropdownButton(
        alignment: Alignment.center,
        hint: const Text(
          'Select viewpoint',
          style: TextStyle(
            color: Colors.deepPurple,
          ),
        ),
        icon: const Icon(Icons.arrow_drop_down),
        iconEnabledColor: Colors.deepPurple,
        iconDisabledColor: Colors.grey,
        style: const TextStyle(color: Colors.deepPurple),
        value: _selectedViewpoint,
        items: _viewpointTitles.map((items) {
          return DropdownMenuItem(
            value: items,
            child: Text(items),
          );
        }).toList(),
        onChanged: (viewpoint) {
          if (viewpoint != null) {
            changeViewpoint(viewpoint);
          }
        },
      ),
    );
  }

  void changeViewpoint(String viewpoint) async {
    // Set the selected viewpoint.
    setState(() => _selectedViewpoint = viewpoint);

    switch (_selectedViewpoint) {
      case 'Geometry':
        // Set Viewpoint using Redlands envelope defined above and a padding of 20.
        await _mapViewController.setViewpointGeometry(
          _redlandsEnvelope.toGeometry(),
          paddingInDiPs: 20,
        );
      case 'Center & Scale':
        // Set Viewpoint so that it is centered on the London coordinates defined above.
        await _mapViewController.setViewpointCenter(
          ArcGISPoint(
            x: -13881.7678417696,
            y: 6710726.57374296,
            spatialReference: SpatialReference.webMercator,
          ),
          scale: 8762.7156655228955,
        );
      case 'Animate':
        if (_fullExtent != null) {
          // Navigate to full extent of the first baselayer before animating to specified geometry.
          _mapViewController.setViewpoint(
            Viewpoint.fromTargetExtent(
              _fullExtent!.extent,
            ),
          );

          // Set Viewpoint of ArcGISMapView to the Viewpoint created above and animate to it using a timespan of 5 seconds.
          await _mapViewController.setViewpointAnimated(
            Viewpoint.fromTargetExtent(_edinburghEnvelope.toGeometry()),
            duration: 5,
          );
        }
      default:
        throw StateError('Unknown viewpoint type');
    }
  }
}

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