Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

GeoView viewpoint synchronization

This code sample is available for these platforms:
View Sample on GitHub

Keep the view points of two views (a MapView and a SceneView in this case) synchronized with each other.

screenshot

Use case

A classic use case is the example of having two map views in one application. One inset map view displays all the layers at their full extent and contains a hollow rectangular graphic that represents the visible extent of the main map view. As the user zooms or pans in the main map view, the extent graphic in the inset map adjust accordingly.

How to use the sample

Interact with either geo view (MapView or the SceneView) by zooming or panning, and the other geo view will automatically set the view point to be the same.

How it works

  1. Wire up the ViewpointChanged and NavigationCompleted 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 view points in multiple event handlers is to account for the various types of interactions that can be occuring on the geo views (ie. single click pan -vs- continuous pan, single click zoom in -vs- mouse scroll wheel zoom, etc.).

Relevant API

  • GeoView
  • GetCurrentViewPoint
  • MapView
  • GeoView.NavigationCompleted
  • GeoView.ViewpointChanged
  • SceneView
  • SetViewPoint

Tags

3D, GetCurrentViewpoint, MapView, SceneView, SetViewpoint, automatic refresh, event, event handler, events, extent, interaction, interactions, pan, sync, synchronize, zoom

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.GeoViewSync.GeoViewSync"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <esriUI:MapView x:Name="MyMapView" Grid.Column="0" />
        <esriUI:SceneView x:Name="MySceneView" Grid.Column="1" />
    </Grid>
</UserControl>
// Copyright 2017 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.

using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using System;

namespace ArcGISRuntime.UWP.Samples.GeoViewSync
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "GeoView viewpoint synchronization",
        "MapView",
        "This sample demonstrates how to keep two geo views (MapView/SceneView) in sync with each other.",
        "")]
    public partial class GeoViewSync
    {
        public GeoViewSync()
        {
            InitializeComponent();

            // Setup the control references and execute initialization
            Initialize();
        }

        private void Initialize()
        {
            // Initialize the MapView and SceneView with a basemap
            MyMapView.Map = new Map(Basemap.CreateImageryWithLabels());
            MySceneView.Scene = new Scene(Basemap.CreateImageryWithLabels());

            // Disable 'flick' gesture - this is the most straightforward way to prevent the 'flick'
            //     animation on one view from competing with user interaction on the other
            MySceneView.InteractionOptions = new SceneViewInteractionOptions { IsFlickEnabled = false };
            MyMapView.InteractionOptions = new MapViewInteractionOptions { IsFlickEnabled = false };

            // Subscribe to viewpoint change events for both views - event raised on click+drag
            MyMapView.ViewpointChanged += OnViewpointChanged;
            MySceneView.ViewpointChanged += OnViewpointChanged;
            
            // Subscribe to the navigation completed events - raised on flick
            MyMapView.NavigationCompleted += OnNavigationComplete;
            MySceneView.NavigationCompleted += OnNavigationComplete;
        }

        private void OnNavigationComplete(object sender, EventArgs eventArgs)
        {
            // Get a reference to the MapView or SceneView that raised the event
            GeoView sendingView = (GeoView)sender;

            // Get a reference to the other view
            GeoView otherView;
            if (sendingView is MapView)
            {
                otherView = MySceneView;
            }
            else
            {
                otherView = MyMapView;
            }

            // Update the viewpoint on the other view
            otherView.SetViewpoint(sendingView.GetCurrentViewpoint(ViewpointType.CenterAndScale));
        }

        private void OnViewpointChanged(object sender, EventArgs e)
        {
            // Get the MapView or SceneView that sent the event
            GeoView sendingView = (GeoView)sender;

            // Only take action if this geoview is the one that the user is navigating.
            // Viewpoint changed events are fired when SetViewpoint is called; This check prevents a feedback loop
            if (sendingView.IsNavigating)
            {
                // If the MapView sent the event, update the SceneView's viewpoint
                if (sender is MapView)
                {
                    // Get the viewpoint
                    Viewpoint updateViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.CenterAndScale);

                    // Set the viewpoint
                    MySceneView.SetViewpoint(updateViewpoint);
                }
                else // Else, update the MapView's viewpoint
                {
                    // Get the viewpoint
                    Viewpoint updateViewpoint = MySceneView.GetCurrentViewpoint(ViewpointType.CenterAndScale);

                    // Set the viewpoint
                    MyMapView.SetViewpoint(updateViewpoint);
                }
            }
        }
    }
}