Set the map’s reference scale and which feature layers should honor the reference scale.

Use case
Setting a reference scale on a Map fixes the size of symbols and text to the desired height and width at that scale. As you zoom in and out, symbols and text will increase or decrease in size accordingly. When no reference scale is set, symbol and text sizes remain the same size relative to the MapView.
Map annotations are typically only relevant at certain scales. For instance, annotations to a map showing a construction site are only relevant at that construction site’s scale. So, when the map is zoomed out that information shouldn’t scale with the MapView, but should instead remain scaled with the Map.
How to use the sample
- Use the drop box at the top to set the map’s reference scale (1:500,000 1:250,000 1:100,000 1:50,000).
- Click the button to set the map scale to the reference scale.
- Use the menu checkboxes in the layer menu to set which feature layers should honor the reference scale.
How it works
- Get and set the reference scale property on the
Mapobject. - Get and set the scale symbols property on each individual
FeatureLayerobject.
Relevant API
- Map
- FeatureLayer
Additional information
The map reference scale should normally be set by the map’s author and not exposed to the end user like it is in this sample.
Tags
map, reference scale, scene
Sample Code
// [WriteFile Name=MapReferenceScale, Category=Maps]// [Legal]// Copyright 2019 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 "MapReferenceScale.h"
// ArcGIS Maps SDK headers#include "Error.h"#include "FeatureLayer.h"#include "Layer.h"#include "LayerListModel.h"#include "Map.h"#include "MapQuickView.h"#include "Portal.h"#include "PortalItem.h"
// Qt headers#include <QFuture>
using namespace Esri::ArcGISRuntime;
MapReferenceScale::MapReferenceScale(QObject* parent /* = nullptr */): QObject(parent), m_portal(new Portal(this)), m_portalItem(new PortalItem(m_portal, "3953413f3bd34e53a42bf70f2937a408", this)){ m_map = new Map(m_portalItem, this);
// Once map is loaded set FeatureLayer list model connect(m_map, &Map::doneLoading, this, [this](const Error& loadError) { if (!loadError.isEmpty()) return;
m_layerInfoListModel = m_map->operationalLayers(); emit layerInfoListModelChanged(); emit currentMapScaleChanged();
connect(m_mapView, &MapQuickView::mapScaleChanged, this, [this]() { emit currentMapScaleChanged(); }); });}
MapReferenceScale::~MapReferenceScale() = default;
void MapReferenceScale::init(){ // Register the map view for QML qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<MapReferenceScale>("Esri.Samples", 1, 0, "MapReferenceScaleSample"); qmlRegisterUncreatableType<QAbstractListModel>("Esri.Samples", 1, 0, "AbstractListModel", "AbstractListModel is uncreateable");}
double MapReferenceScale::currentMapScale() const{ if (!m_mapView) return 0.0;
return m_mapView->mapScale();}
void MapReferenceScale::setCurrentMapScale(double scale){ if(!m_map) return;
m_map->setReferenceScale(scale);}
void MapReferenceScale::setMapScaleToReferenceScale(double scale){ if(m_mapView) m_mapView->setViewpointScaleAsync(scale);}
void MapReferenceScale::featureLayerScaleSymbols(const QString& layerName, bool checkedStatus){ if(m_layerInfoListModel) { for(Layer* layer : *static_cast<LayerListModel*>(m_layerInfoListModel)) { if(layer->name() == layerName) { FeatureLayer* featureLayer = static_cast<FeatureLayer*>(layer); if(featureLayer) featureLayer->setScaleSymbols(checkedStatus); } } }}
MapQuickView* MapReferenceScale::mapView() const{ return m_mapView;}
// Set the view (created in QML)void MapReferenceScale::setMapView(MapQuickView* mapView){ if (!mapView || mapView == m_mapView) return;
m_mapView = mapView; m_mapView->setMap(m_map);
emit mapViewChanged();}// [WriteFile Name=MapReferenceScale, Category=Maps]// [Legal]// Copyright 2019 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 MAPREFERENCESCALE_H#define MAPREFERENCESCALE_H
// Qt headers#include <QAbstractListModel>#include <QObject>
namespace Esri::ArcGISRuntime{class Map;class MapQuickView;class Portal;class PortalItem;class LayerListModel;}
Q_MOC_INCLUDE("MapQuickView.h")
class MapReferenceScale : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(QAbstractListModel* layerInfoListModel MEMBER m_layerInfoListModel NOTIFY layerInfoListModelChanged) Q_PROPERTY(double currentMapScale READ currentMapScale WRITE setCurrentMapScale NOTIFY currentMapScaleChanged)
public: explicit MapReferenceScale(QObject* parent = nullptr); ~MapReferenceScale();
static void init();
Q_INVOKABLE void setMapScaleToReferenceScale(double scale); Q_INVOKABLE void featureLayerScaleSymbols(const QString& layerName, bool checkedStatus);
signals: void mapViewChanged(); void layerInfoListModelChanged(); void currentMapScaleChanged();
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); void setCurrentMapScale(double scale); double currentMapScale() const;
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::Portal* m_portal = nullptr; Esri::ArcGISRuntime::PortalItem* m_portalItem = nullptr; QAbstractListModel* m_layerInfoListModel = nullptr;};
#endif // MAPREFERENCESCALE_H// [WriteFile Name=MapReferenceScale, Category=Maps]// [Legal]// Copyright 2019 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.Samplesimport QtQuick.Layouts
Item { readonly property var referenceScales: [500000, 250000, 100000, 50000]
MapView { id: myMapView anchors.fill: parent
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); } }
Rectangle { anchors { margins: 5 left: parent.left top: parent.top } width: childrenRect.width height: childrenRect.height color: "#000000" opacity: .75 radius: 5
ColumnLayout { Text { color: "#ffffff" text: "Current Map Scale 1:%1".arg(Math.round(mapReferenceScaleSampleModel.currentMapScale)) Layout.fillWidth: true Layout.margins: 3 font { weight: Font.DemiBold pointSize: 10 } }
Text { color: "#ffffff" text: qsTr("Select a new reference scale") Layout.fillWidth: true Layout.margins: 3 font { weight: Font.DemiBold pointSize: 10 } }
ComboBox { id: scales font { weight: Font.DemiBold pointSize: 10 } Layout.margins: 3 Layout.fillWidth: true model: ["1:500000","1:250000","1:100000","1:50000"] Component.onCompleted: mapReferenceScaleSampleModel.currentMapScale = referenceScales[scales.currentIndex]; onActivated: mapReferenceScaleSampleModel.currentMapScale = referenceScales[scales.currentIndex];
// 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 } }
Button { text: qsTr("Set Map Scale to Reference Scale") font { weight: Font.DemiBold pointSize: 10 } Layout.margins: 3 Layout.fillWidth: true onClicked: mapReferenceScaleSampleModel.setMapScaleToReferenceScale(referenceScales[scales.currentIndex]); } } }
Rectangle { anchors { margins: 5 right: parent.right top: parent.top } width: childrenRect.width height: childrenRect.height color: "#000000" opacity: .75 radius: 5
ColumnLayout { Text { text: qsTr("Apply Reference Scale") horizontalAlignment: Text.AlignHCenter Layout.fillWidth: true Layout.margins: 2 font { weight: Font.DemiBold pointSize: 10 } color: "#ffffff" }
Repeater { // Assign the model to the list model of operational layers id: featureLayerRepeater model: mapReferenceScaleSampleModel.layerInfoListModel width: childrenRect.width height: childrenRect.height
// Assign the delegate to display text next to checkbox as a row delegate: Row { CheckBox { id: featureLayerBox checked: true onCheckStateChanged: mapReferenceScaleSampleModel.featureLayerScaleSymbols(name,featureLayerBox.checked); } Text { id: featureLayerText text: name height: featureLayerBox.height verticalAlignment: Text.AlignVCenter font.pointSize: 10 color: "#ffffff" } } } } } // Declare the C++ instance which creates the map etc. and supply the view MapReferenceScaleSample { id: mapReferenceScaleSampleModel mapView: myMapView }}// [Legal]// Copyright 2019 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 "MapReferenceScale.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
// 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("MapReferenceScale"));
// 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 MapReferenceScale::init();
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
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path engine.addImportPath(arcGISRuntimeImportPath);
// Set the source engine.load(QUrl("qrc:/Samples/Maps/MapReferenceScale/main.qml"));
return app.exec();}// Copyright 2019 ESRI//// All rights reserved under the copyright laws of the United States// and applicable international laws, treaties, and conventions.//// You may freely redistribute and use this sample code, with or// without modification, provided you include the original copyright// notice and use restrictions.//// See the Sample code usage restrictions document for further information.//
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
MapReferenceScale { anchors.fill: parent }}