Search code examples
qtqmlarcgisarcgis-runtime

How to retrieve map scale using ArcGIS for QML/QT?


I am building a simple QML application that makes use of the ArcGIS SDK, the purpose right now is just to learn the SDK features. What I'd like to do is have a zoomable map and a text box that displays the current map scale. Below is the code I have written for this, based upon the samples on the ArcGIS website.

import QtQuick 2.6
import QtQuick.Controls 1.4
import Esri.ArcGISRuntime 100.1

ApplicationWindow {
    id: appWindow
    width: 800
    height: 600
    title: "Untitled"
    MapView {
        id: mainmapview
        attributionTextVisible: false
        anchors.topMargin: 0
        anchors.rightMargin: 0
        anchors.fill: parent
        focus: true
        Map {
            id: mainmap
            BasemapLightGrayCanvasVector {}
        }
        onMapScaleChanged: scaletext.text=mainmapview.scale.toString()

        Text {
            id: scaletext
            x: 10
            y: 10
            width: 285
            height: 45
            text: qsTr("Text")
            font.pixelSize: 12
        }
    }
}

The map loads and I can see it OK, but the scale textbox doesn't work: it always shows the scale as '1', no matter how much I zoom in or out. Obviously this isn't correct. Am I messing up the type conversion to text?

Any pointers as to how to resolve this would be great. Thanks.


Solution

  • Actually I figured it out myself. This line:

    onMapScaleChanged: scaletext.text=mainmapview.scale.toString()
    

    Should be:

    onMapScaleChanged: scaletext.text=mainmapview.mapScale.toString()
    

    Rather than delete the question I've answered it in case anyone else has the same problem and can find this through google.