Invalid viewpoint value in onViewpointChanged event

409
3
09-01-2023 05:28 AM
Labels (3)
by Anonymous User
Not applicable

Hi,

My application MapView catches onViewpointChanged event and makes some calculations. Simplified code looks like below:

 

        MapView {
            id: mapView

            onViewpointChanged: {
                updateSomeInfo()
            }

            function updateSomeInfo() {
                if(typeof mapView.currentViewpointCenter === "undefined" || mapView.currentViewpointCenter == null) {
                    return
                }
                if(!mapView.currentViewpointCenter) {
                    return
                }

                // error here
                var centerPoint = mapView.currentViewpointCenter.center

            }
        }

 

Application sometimes returns error on line #17: TypeError: Cannot read property 'center' of null

I don't know how I need to check availability of mapView.currentViewpointCenter property value and don't get error. It looks like that mapView.currentViewpointCenter content changes in my event processing time.

Can someone help me?

 

0 Kudos
3 Replies
JamesBallard1
Esri Regular Contributor

Hi @Anonymous User. I gave your code a spin on macOS with Qt 5.15.2 using our 100.15.0 release. It looks ok for me. I modified one of our out-of-the-box templates with your code and it prints fine. Are there any additional details you can share so we can help troubleshoot?

Here's what I tried. I also applied an API key locally for testing.

import QtQuick 2.12
import QtQuick.Controls 2.12
import Esri.ArcGISRuntime 100.15

ApplicationWindow {
    id: appWindow
    width: 800
    height: 600
    title: "Untitled445"

    MapView {
        id: mapView
        anchors.fill: parent
        focus: true

        onViewpointChanged: {
              updateSomeInfo()
          }

          function updateSomeInfo() {
              if (!mapView.currentViewpointCenter) {
                  console.log("it's null");
                  return;
              }

              var centerPoint = mapView.currentViewpointCenter.center;
              console.log(JSON.stringify(centerPoint.json));
          }

        Map {
            initBasemapStyle: Enums.BasemapStyleArcGISStreets
        }
    }
}

 

As soon as it starts and I begin interacting the map, I see the logging:

Initializing application
Esri.ArcGISRuntime.Plugin: virtual void ArcGISRuntimePlugin::registerTypes(const char *) Esri.ArcGISRuntime
Esri.ArcGISRuntime.Plugin: static void QmlUtils::registerTypes(const char *) Esri.ArcGISRuntime
qml: {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":0,"y":0}
qml: {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":0,"y":0}
qml: {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":-1740.0984933674335,"y":509.2080693356693}
qml: {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":-697460.4504242502,"y":204099.07298399135}
qml: {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":-701574.0835914724,"y":205318.75499369204}
qml: {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":-704368.3717440777,"y":206153.8160307184}
qml: {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":-1326097.7183212154,"y":391954.96628710814}
qml: {"spatialReference":{"latestWkid":3857,"wkid":102100},"x":-1327220.7128490992,"y":392293.60304021835}

0 Kudos
GKmieliauskas
Esri Regular Contributor

I have found that error only on different Android devices. It happens very rarely. Application is used for river navigation so that event fires many times during navigation time.

I have added different types of checking for validity of mapView.currentViewpointCenter. But error still happens. In one of ArcGIS AppStudio samples I have found code where mapView.currentViewpointCenter assigns to const:

const viewpointCenter = mapView.currentViewpointCenter

Then uses assigned value in code.

I am not expert in QT but maybe it could be way to avoid error.

0 Kudos
JamesBallard1
Esri Regular Contributor

I would not think that var vs const would make a difference, but as I am not able to reproduce the problem I'd suggest going with that if it works for you. I recommend reaching out to Esri support if you are still hitting problems related to specific Android devices.

0 Kudos