Set the map view to a new viewpoint.

Use case
Programatically 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
- Create a new
Mapobject and set it to theMapViewobject. - Change the map’s
Viewpointusing one of the available methods:
- Use
MapView::setViewpointAsync(viewPoint, duration, AnimationCurve)to pan to a viewpoint over the specified length of time. - Use
MapView::setViewpointCenterAsync()to center the viewpoint on aPointand set a distance from the ground using a scale. - Use
MapView::setViewpointGeometryAsync()to set the viewpoint to a givenGeometry - Use
MapView::setViewpointRotationAsync()to set the viewpoint to a given degree. - Use
MapView::setViewpointScaleAsync()to set the viewpoint to a given scale.
Relevant API
- AnimationCurve
- Geometry
- Map
- MapView
- Point
- Viewpoint
Additional information
Below are some other ways to set a viewpoint:
- GeoView::setViewpointAsync
Tags
animate, extent, pan, rotate, scale, view, zoom
Sample Code
// [WriteFile Name=ChangeViewpoint, Category=Maps]// [Legal]// Copyright 2016 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.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "ChangeViewpoint.h"
// ArcGIS Maps SDK headers#include "Basemap.h"#include "Envelope.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MapViewTypes.h"#include "Point.h"#include "SpatialReference.h"#include "Viewpoint.h"
// Qt headers#include <QFuture>
using namespace Esri::ArcGISRuntime;
ChangeViewpoint::ChangeViewpoint(QQuickItem* parent) : QQuickItem(parent){}
ChangeViewpoint::~ChangeViewpoint() = default;
void ChangeViewpoint::init(){ qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<ChangeViewpoint>("Esri.Samples", 1, 0, "ChangeViewpointSample");}
void ChangeViewpoint::componentComplete(){ QQuickItem::componentComplete();
// create a new basemap instance Basemap* basemap = new Basemap(BasemapStyle::ArcGISImagery, this); // create a new map instance m_map = new Map(basemap, this); // set map on the map view m_mapView->setMap(m_map);}
void ChangeViewpoint::changeViewpoint(QString viewpoint){ if (viewpoint == "Center") { Point ptEsriHeadquarters(-117.195681,34.056218, SpatialReference(4326)); m_mapView->setViewpointCenterAsync(ptEsriHeadquarters); } else if (viewpoint == "Center and scale") { Point ptHawaii(-157.564, 20.677, SpatialReference(4236)); m_mapView->setViewpointCenterAsync(ptHawaii, 4000000.0); } else if (viewpoint == "Geometry") { Envelope envBeijing(116.380, 39.920, 116.400, 39.940, SpatialReference(4236)); m_mapView->setViewpointGeometryAsync(envBeijing); } else if (viewpoint == "Geometry and padding") { Envelope envBeijing(116.380, 39.920, 116.400, 39.940, SpatialReference(4236)); m_mapView->setViewpointGeometryAsync(envBeijing, 200 * screenRatio()); } else if (viewpoint == "Rotation") { m_rotationValue = (m_rotationValue + 45) % 360; m_mapView->setViewpointRotationAsync(m_rotationValue); } else if (viewpoint == "Scale 1:5,000,000") { m_mapView->setViewpointScaleAsync(5000000.0); } else if (viewpoint == "Scale 1:10,000,000") { m_mapView->setViewpointScaleAsync(10000000.0); } else if (viewpoint == "Animation") { //! [set viewpoint api snippet] constexpr double xMin = -12338668.348591767; constexpr double yMin = 5546908.424239618; constexpr double xMax = -12338247.594362013; constexpr double yMax = 5547223.989911933; constexpr int wkid = 102100; Viewpoint vpSpring(Envelope(xMin, yMin, xMax, yMax, SpatialReference(wkid))); constexpr float duration = 4.0f; m_mapView->setViewpointAsync(vpSpring, duration, AnimationCurve::EaseInOutCubic); //! [set viewpoint api snippet] }}
MapQuickView* ChangeViewpoint::mapQuickView() const{ return m_mapView;}
void ChangeViewpoint::setMapQuickView(MapQuickView* mapView){ m_mapView = mapView; emit mapQuickViewChanged();}
double ChangeViewpoint::screenRatio() const{ const double width = static_cast<double>(m_mapView->width()); const double height = static_cast<double>(m_mapView->height()); return height > width ? width / height : height / width;}// [WriteFile Name=ChangeViewpoint, Category=Maps]// [Legal]// Copyright 2016 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.// [Legal]
#ifndef CHANGE_VIEWPOINT_H#define CHANGE_VIEWPOINT_H
// Qt headers#include <QQuickItem>
namespace Esri::ArcGISRuntime{ class Map; class MapQuickView;}
Q_MOC_INCLUDE("MapQuickView.h")
class ChangeViewpoint : public QQuickItem{ Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapQuickView WRITE setMapQuickView NOTIFY mapQuickViewChanged) Q_OBJECT
public: explicit ChangeViewpoint(QQuickItem* parent = nullptr); ~ChangeViewpoint() override;
void componentComplete() override; static void init(); Q_INVOKABLE void changeViewpoint(QString viewpoint);
signals: void mapQuickViewChanged();
private: Esri::ArcGISRuntime::MapQuickView* mapQuickView() const; void setMapQuickView(Esri::ArcGISRuntime::MapQuickView* mapView);
double screenRatio() const;
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; int m_rotationValue = 0;};
#endif // CHANGE_VIEWPOINT_H// [WriteFile Name=ChangeViewpoint, Category=Maps]// [Legal]// Copyright 2016 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.// [Legal]
import QtQuickimport QtQuick.Controlsimport Esri.Samples
ChangeViewpointSample { id: changeViewpointSample width: 800 height: 600
// add a mapView component MapView { id: mapQuickView anchors.fill: parent
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } } mapView: mapQuickView
ComboBox { id: comboBoxViewpoint anchors { left: parent.left top: parent.top margins: 15 }
// Add a background to the ComboBox Rectangle { anchors.fill: parent radius: 10 // Make the rectangle visible if a dropdown indicator exists // An indicator only exists if a theme is set visible: parent.indicator border.width: 1 }
property int bestWidth: implicitWidth
width: bestWidth + rightPadding + leftPadding + 20
model: [ "Center", "Center and scale", "Geometry", "Geometry and padding", "Rotation", "Scale 1:5,000,000", "Scale 1:10,000,000", "Animation" ]
onCurrentTextChanged: { // Call C++ invokable function to change the viewpoint changeViewpointSample.changeViewpoint(comboBoxViewpoint.currentText); }
onModelChanged: { let w = bestWidth; for (let i = 0; i < comboBoxViewpoint.model.length; ++i) { metrics.text = comboBoxViewpoint.model[i]; w = Math.max(w, metrics.width); } bestWidth = w; }
TextMetrics { id: metrics font: comboBoxViewpoint.font } }}// [Legal]// Copyright 2015 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.// [Legal]
// sample headers#include "ChangeViewpoint.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlEngine>#include <QQuickView>#include <QSettings>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("ChangeViewpoint"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample ChangeViewpoint::init();
// Initialize application view QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView);
// Add the import Path view.engine()->addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive); #endif
// Add the Runtime and Extras path view.engine()->addImportPath(arcGISRuntimeImportPath);
// Set the source view.setSource(QUrl("qrc:/Samples/Maps/ChangeViewpoint/ChangeViewpoint.qml"));
view.show();
return app.exec();}