我有一个自定义的图表主机,在目录结构中有几个平铺的地图:
http://host/New_York/
http://host/Washington/
http://host/Montreal/
QML应用程序具有一个ComboBox组件,允许用户选择他要显示的图表。
Map组件使用osm
插件,其PluginParameter指向用于图表的URL。我以为我可以为该PluginParameter动态分配一个值,但是它不起作用,即使分配了该值,它也保持不变。我还尝试销毁了Plugin对象,将其重新创建并将其分配给Map对象,但是我收到一条错误消息,指出plugin
属性为ReadOnly
。
动态更改Map组件使用的Plugin对象的自定义主机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
...
该插件只能编写一次,因此以后不能更改,因此,您将必须使用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]
}