setViewpointRotation() not working properly?

461
2
Jump to solution
10-08-2023 06:49 PM
Labels (2)
smithse
New Contributor III

Hi all,

I have a button on my app which is intended to return the user to a 'home' view or default map view.

I can get the extent part to work, but trying to get the map to return to 'North' up, is nigh on impossible. What am I missing?

To force the map to a rotation other than north, on the 'onMapChanged' event, I force the map to rotate to 45° by setting:

//dev
mapView.setViewpointRotation(45)

 

When I tap the home button, whose code is below, the extent returns, but the rotation doesn't change.

function resetToHomeView() {
    console.log("Resetting view")
    mapView.setViewpointRotation(mapView.initialMapRotation)
    mapView.setViewpoint(bwiViewPoint)
}

For clarity, there is the following property on the mapView:

property real initialMapRotation: 0

 

Any feedback would be greatly welcomed!

Cheers,
Sean

 

1 Solution

Accepted Solutions
LucasDanzinger
Esri Frequent Contributor

Hi Sean,

Based on the resetToHomeView code, there are 2 setViewpoint calls one after another. However, these are async calls, so you may be running into some behavior where the previous setViewpoint is canceled if it's not complete yet. One idea to try would be to chain them up to run once the other one is complete. Here is some sample code I tried that works (not the prettiest, but you could probably make it more robust)

 

// add a mapView component
MapView {
id: mv
anchors.fill: parent
// set focus to enable keyboard navigation
focus: true

// add a map to the mapview
Map {
    // add the ArcGISStreets basemap to the map
    initBasemapStyle: Enums.BasemapStyleArcGISStreets

    initialViewpoint: ViewpointExtent {
        extent: Envelope {
            id: env
            json: {"spatialReference":{"latestWkid":3857,"wkid":102100},"xmax":-13013797.089479687,"xmin":-13034963.75614633,"ymax":4036739.5261344,"ymin":4020864.5261344174}
        }
    }
}

onViewpointChanged: {

    if (isResetting && mv.mapRotation === 0) {
        console.log("in here")
        isResetting = false;
        mv.setViewpointGeometry(env);
    }
}

Button {
    text: "reset"
    onClicked: {
        if (mv.mapRotation === 0) {
            mv.setViewpointGeometry(env)
        }

        isResetting = true
        mv.setViewpointRotation(0)
    }
}

View solution in original post

2 Replies
LucasDanzinger
Esri Frequent Contributor

Hi Sean,

Based on the resetToHomeView code, there are 2 setViewpoint calls one after another. However, these are async calls, so you may be running into some behavior where the previous setViewpoint is canceled if it's not complete yet. One idea to try would be to chain them up to run once the other one is complete. Here is some sample code I tried that works (not the prettiest, but you could probably make it more robust)

 

// add a mapView component
MapView {
id: mv
anchors.fill: parent
// set focus to enable keyboard navigation
focus: true

// add a map to the mapview
Map {
    // add the ArcGISStreets basemap to the map
    initBasemapStyle: Enums.BasemapStyleArcGISStreets

    initialViewpoint: ViewpointExtent {
        extent: Envelope {
            id: env
            json: {"spatialReference":{"latestWkid":3857,"wkid":102100},"xmax":-13013797.089479687,"xmin":-13034963.75614633,"ymax":4036739.5261344,"ymin":4020864.5261344174}
        }
    }
}

onViewpointChanged: {

    if (isResetting && mv.mapRotation === 0) {
        console.log("in here")
        isResetting = false;
        mv.setViewpointGeometry(env);
    }
}

Button {
    text: "reset"
    onClicked: {
        if (mv.mapRotation === 0) {
            mv.setViewpointGeometry(env)
        }

        isResetting = true
        mv.setViewpointRotation(0)
    }
}
smithse
New Contributor III

Thanks Lucas, this was the answer. I did wonder if async was the reason! Live and learn haha.

0 Kudos