0

I want to change map.activeMapType at the app startup. I have menu mapTypeMenu with actions that change activeMapType. So I added Component.onCompleted to the MapView to trigger the needed action to switch activeMapType. But it looks like map switches to the default (street map) activeMapType again after this.

ApplicationWindow {
MapView {
        id: mapview
        anchors.fill: parent
        map.plugin: Plugin { name: "osm" }
        map.zoomLevel: 4

        Component.onCompleted: {
            for (var i = 0; i < mapTypeMenu.count; i++) {
                if (mapTypeMenu.actionAt(i).text == "Terrain Map") {
                    console.log("triggered")
                    mapTypeMenu.actionAt(i).trigger();
                    console.log(mapTypeMenu.actionAt(i).text)
                }
            }
            console.log("map compl")
        }
}
}

When action from mapTypeMenu triggers, called following code:

onSelectMapType: (mapType) => {
    for (var i = 0; i < mapTypeMenu.count; i++) { 
       mapTypeMenu.actionAt(i).checked = mapTypeMenu.actionAt(i).text === mapType.name
    }
    mapview.map.activeMapType = mapType
    console.log("Done")
}

Output:

qml: window compl
qml: triggered
qml: Done
qml: Terrain Map
qml: map compl
QGeoTileProviderOsm: Tileserver disabled at  QUrl("http://maps-redirect.qt.io/osm/5.8/satellite")
QGeoTileFetcherOsm: all providers resolved

I was able to find a solution to the problem only by using a timer:

    Timer {
        interval: 2
        onTriggered: {
            for (var i = 0; i < mapTypeMenu.count; i++) {
                if (mapTypeMenu.actionAt(i).text === "Terrain Map") {
                    mapTypeMenu.actionAt(i).trigger();
                    console.log("Triggered: " + mapTypeMenu.actionAt(i).text);
                }
            }
        }
        running: true
        repeat: false
    }

But is there a better solution?

kancler
  • 81
  • 1
  • 8
  • 1
    I'm currently looking into it and have the same issue. I think it is because the plugin resets the `supportedMapTypes` as soon as it finds out that the satellite map isn't supported. The reset might trigger a reset of the `activeMapType` property, hence deferring it works in your case. – iam_peter Jul 14 '23 at 10:25
  • 1
    By the way, I wouldn't trigger the menu action to change the map type manually instead set the `activeMapType` directly and check the related action in the menu accordingly. – iam_peter Jul 14 '23 at 10:26

1 Answers1

1

As already written in the comments on your question the issue is

QGeoTileProviderOsm: Tileserver disabled at  QUrl("http://maps-redirect.qt.io/osm/5.8/satellite")
QGeoTileFetcherOsm: all providers resolved

which basically re-generates the supportedMapTypes and thus resetting the activeMapType, so all previous set map types are overwritten.

I found a solution for it here. This will add PluginParameter objects to the Plugin object in order to avoid the re-generation of the available providers. You can read more about this here (also linked in the forum post).

ApplicationWindow {
    id: root
    width: 640
    height: 480
    visible: true
    title: qsTr("Map")

    property int activeMapTypeIndex: 0
    property string startupMapTypeName: "Terrain Map"

    onActiveMapTypeIndexChanged: {
        mapView.map.activeMapType = mapView.map.supportedMapTypes[root.activeMapTypeIndex]
    }

    Component.onCompleted: {
        let supportedMapTypes = mapView.map.supportedMapTypes

        for (let i = 0; i < supportedMapTypes.length; ++i) {
            if (supportedMapTypes[i].name === root.startupMapTypeName) {
                root.activeMapTypeIndex = i
                return
            }
        }

        console.log("Couldn't find start up map type", root.startupMapTypeName)
    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("Map Types")

            Repeater {
                model: mapView.map.supportedMapTypes

                MenuItem {
                    checkable: true
                    checked: root.activeMapTypeIndex === index
                    text: modelData.name
                    onTriggered: root.activeMapTypeIndex = index
                }
            }
        }
    }

    MapView {
        id: mapView
        anchors.fill: parent
        map {
            plugin: Plugin {
                name: "osm"

                PluginParameter {
                    name: "osm.mapping.providersrepository.disabled"
                    value: "true"
                }

                PluginParameter {
                    name: "osm.mapping.providersrepository.address"
                    value: "http://maps-redirect.qt.io/osm/5.6/"
                }
            }
            center: QtPositioning.coordinate(59.91, 10.75) // Oslo
            zoomLevel: 14
        }
    }
}
iam_peter
  • 3,205
  • 1
  • 19
  • 31