Skip to content

Match viewpoint of geo views

View on GitHub

Keep the viewpoints of two views -- for example, a map view and a scene view -- synchronized with each other.

Image of match viewpoint of geoviews

Use case

You might need to synchronize GeoView viewpoints if you had two map views in one application - a main map and an inset. An inset map view could display all the layers at their full extent and contain a hollow rectangular graphic that represents the visible extent of the main map view. As you zoom or pan in the main map view, the extent graphic in the inset map would adjust accordingly.

How to use the sample

Interact with the MapView or SceneView by zooming or panning. The other MapView or SceneView will automatically focus on the same viewpoint.

How it works

  1. Wire up the onViewpointChanged event handlers for both geo views.
  2. In each event handler, get the current viewpoint from the geo view that is being interacted with and then set the viewpoint of the other geo view to the same value.
  3. Note: The reason for setting the viewpoints in multiple event handlers is to account for different types of interactions that can occur (ie. single click pan -vs- continuous pan, single click zoom in -vs- mouse scroll wheel zoom, etc.).

Relevant API

  • GeoViewController
  • getCurrentViewpoint
  • MapView
  • onNavigationChanged
  • onViewpointChanged
  • SceneView
  • setViewpoint

About the data

This application provides two different perspectives of the Imagery basemap. A 2D MapView as well as a 3D SceneView, displayed side by side.

Tags

3D, automatic refresh, event, event handler, events, extent, interaction, interactions, pan, zoom

Sample Code

match_viewpoint_of_geo_views.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
// 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
//
//   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 'dart:async';

import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart';
import 'package:flutter/material.dart';

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

  @override
  State<MatchViewpointOfGeoViews> createState() =>
      _MatchViewpointOfGeoViewsState();
}

class _MatchViewpointOfGeoViewsState extends State<MatchViewpointOfGeoViews>
    with SampleStateSupport {
  // Create a controller for the map view.
  final _mapViewController = ArcGISMapView.createController();
  // A controller for the scene view.
  final _sceneViewController = ArcGISSceneView.createController();
  // A flag to indicate if the map view is currently interacting.
  bool _isMapViewInteraction = false;
  // A flag to indicate if the scene view is currently interacting.
  bool _isSceneViewInteraction = false;
  // Stream subscriptions for viewpoint changes.
  late StreamSubscription _mapViewViewpointChangedSubscription;
  late StreamSubscription _sceneViewViewpointChangedSubscription;
  // A flag for when the map view is ready and controls can be used.
  var _ready = false;

  @override
  void dispose() {
    _mapViewViewpointChangedSubscription.cancel();
    _sceneViewViewpointChangedSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final isPortrait =
        MediaQuery.of(context).orientation == Orientation.portrait;

    return Scaffold(
      body: SafeArea(
        top: false,
        left: false,
        right: false,
        child: Stack(
          children: [
            if (isPortrait)
              Column(children: getViews())
            else
              Row(children: getViews()),

            // Display a progress indicator and prevent interaction until state is ready.
            LoadingIndicator(visible: !_ready),
          ],
        ),
      ),
    );
  }

  void updateViewInteraction(bool isMapViewInteraction) {
    setState(() {
      _isMapViewInteraction = isMapViewInteraction;
      _isSceneViewInteraction = !isMapViewInteraction;
    });
  }

  List<Widget> getViews() {
    return [
      // Add a map view to the widget tree and set a controller.
      Expanded(
        child: GestureDetector(
          behavior: HitTestBehavior.deferToChild,
          onTapDown: (_) => updateViewInteraction(true),
          onDoubleTapDown: (_) => updateViewInteraction(true),
          onLongPressDown: (_) => updateViewInteraction(true),
          onScaleStart:(_) => updateViewInteraction(true),
          child: ArcGISMapView(
            controllerProvider: () => _mapViewController,
            onMapViewReady: onMapViewReady,
          ),
        ),
      ),
      // Add a scene view to the widget tree and set a controller.
      Expanded(
        child: GestureDetector(
          behavior: HitTestBehavior.deferToChild,
          onTapDown: (_) => updateViewInteraction(false),
          onDoubleTapDown: (_) => updateViewInteraction(false),
          onLongPressDown: (_) => updateViewInteraction(false),
          onScaleStart:(_) => updateViewInteraction(false),
          child: ArcGISSceneView(
            controllerProvider: () => _sceneViewController,
            onSceneViewReady: onSceneViewReady,
          ),
        ),
      ),
    ];
  }

  void onMapViewReady() {
    setState(() => _ready = false);
    // Create a map with a topographic basemap style.
    final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISImagery);
    _mapViewController.arcGISMap = map;

    // Listen for viewpoint changes in the map view.
    _mapViewViewpointChangedSubscription = _mapViewController.onViewpointChanged
        .listen((_) {
          if (_isMapViewInteraction) {
            final mapViewViewpoint = _mapViewController.getCurrentViewpoint(
              ViewpointType.centerAndScale,
            );
            _sceneViewController.setViewpoint(mapViewViewpoint!);
          }
        });

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

  void onSceneViewReady() {
    setState(() => _ready = false);
    // Create a scene with a topographic basemap style.
    final scene = ArcGISScene.withBasemapStyle(BasemapStyle.arcGISImagery);
    _sceneViewController.arcGISScene = scene;

    // Listen for viewpoint changes in the scene view.
    // This will update the map view's viewpoint when the scene view changes.
    _sceneViewViewpointChangedSubscription = _sceneViewController
        .onViewpointChanged
        .listen((_) {
          if (_isSceneViewInteraction) {
            final sceneViewViewpoint = _sceneViewController.getCurrentViewpoint(
              ViewpointType.centerAndScale,
            );
            _mapViewController.setViewpoint(sceneViewViewpoint!);
          }
        });

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

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