我尝试将 QML 地图项(如 C++ 中的 MapQuickItem 或 MapCircle)添加到我的地图中。不幸的是,它们没有出现在地图上。只有一个 QML 矩形的相同代码可以工作。rect v1 是用于测试哪个有效的矩形。rect v2 是不起作用的 mapCircle。当我将我的圆形 QML 代码 1:1 添加到我的地图 QML 代码中时,它工作正常。
主.cpp:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView *view = new QQuickView;
view->setSource(QUrl("qrc:/map.qml"));
QQmlComponent compRect (view->engine(), QUrl("qrc:/rect.qml"));
view->setWidth(1000);
view->setHeight(650);
view->setTitle("GUI");
QQuickItem *map = view->findChild<QQuickItem*>("map1");
QQuickItem *rect = qobject_cast<QQuickItem*>(compRect.create(view->rootContext()));
rect->setParentItem(map);
rect->setParent(map); //know this is not for visual objects, just for test
view->show();
return app.exec();
}
rect.qml v1
import QtQuick 2.14
import QtLocation 5.14
import QtPositioning 5.14
Rectangle
{
color: "grey"
opacity: .8
width: 100
height: 100
radius: 4
Text
{
id: text
anchors.centerIn: parent
text: "hi"
color: "orangered"
font.weight: Font.Bold
}
}
rect.qml v2
import QtQuick 2.14
import QtLocation 5.14
import QtPositioning 5.14
MapCircle
{
center
{
latitude: 47.5
longitude: 8.9
}
radius: 5000.0
color: 'green'
border.width: 3
}
地图.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtPositioning 5.14
import QtLocation 5.14
Map
{
objectName: "map1"
id: map_map
anchors.centerIn: parent;
anchors.fill: parent
plugin: Plugin
{
name: "mapboxgl" // "osm", "esri", ...
}
center: QtPositioning.coordinate(47.6,9.5)
zoomLevel: 8
// get position of device and set map center to it
PositionSource
{
active: true
onPositionChanged:
{
map.center(position.coordinate)
console.log(position.coordinate)
}
}
Timer
{
id: timerReload
interval: 1000
repeat: true
running: true
onTriggered:
{
controller.triggerReload();
}
}
}