在 QML 映射中动态更改 osm 插件的自定义主机 URL

Dynamically change custom host URL of osm Plugin in a QML Map

我有一个自定义图表主机,在目录结构中有几个平铺地图:

http://host/New_York/
http://host/Washington/
http://host/Montreal/

QML 应用程序有一个 ComboBox 组件,允许用户select他想显示哪个图表。

地图组件使用 osm 插件和指向 URL 的 PluginParameter 以用于图表。我以为我可以简单地为这个 PluginParameter 动态分配一个值,但是它不起作用,即使分配它,该值仍然保持不变。我还尝试销毁 Plugin 对象,重新创建它并将其分配给 Map 对象,但我收到一条错误消息,指出 plugin 属性 是 ReadOnly.

动态更改地图组件使用的插件对象的自定义主机 URL 的正确方法是什么?

    Plugin {
        id: mapPlugin
        name: "osm"

        PluginParameter { id: charturl; name: "osm.mapping.custom.host"; }
    }

    Map {
        id: mapview
        plugin: mapPlugin
        activeMapType: supportedMapTypes[supportedMapTypes.length - 1]
...

    ComboBox {
...
        onCurrentIndexChanged: {
            charturl.value = cbItems.get(currentIndex).url
...

Plugin只能写一次,所以以后不能改,所以这里要用Loader新建地图:

main.qml

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14

import QtLocation 5.14
import QtPositioning 5.14

Window {
    visible: true
    width: 640
    height: 480
    ColumnLayout {
        anchors.fill: parent
        ComboBox {
            id: combobox
            model: [
                "http://host/New_York/",
                "http://host/Washington/",
                "http://host/Montreal/"
            ]
            Layout.fillWidth: true
            onActivated: changeHost()
        }
        Loader{
            id: loader
            Layout.fillWidth: true
            Layout.fillHeight: true
            onStatusChanged: if (loader.status === Loader.Ready) console.log('Loaded')
        }
        Component.onCompleted: changeHost()
    }
    function changeHost(){
        var item = loader.item
        var zoomLevel = item ? item.zoomLevel: 14
        var center = item ? item.center: QtPositioning.coordinate(59.91, 10.75)

        loader.setSource("MapComponent.qml", {
                             "host": combobox.currentValue,
                             "center": center,
                             "zoomLevel": zoomLevel}
                         )
    }
}

MapComponent.qml

import QtLocation 5.14

Map {
    id: map
    property string host: ""
    plugin: Plugin {
        name: "osm"
        PluginParameter {
            name: "osm.mapping.custom.host"
            value: map.host
        }
    }
    activeMapType: supportedMapTypes[supportedMapTypes.length - 1]
}