QML 地图在每次单击时显示指针图像



我有这样的代码

Map {
    id: map
    anchors.fill: parent
    plugin: Plugin {
        name: "osm"
    }
    center: QtPositioning.coordinate(59.91, 10.75)
    zoomLevel: 10
    MapQuickItem {
        id: transMarker
        sourceItem: Image {
            id: transImage
            width: 50
            height: 50
            source: "trans.png"
        }
    }
    MouseArea{
        anchors.fill: parent
        onClicked: {
            var coord = map.toCoordinate(Qt.point(mouse.x,mouse.y));
            transMarker.coordinate = coord;
            console.log(coord.latitude, coord.longitude)
        }
    }
}

因此,现在每次单击时,图像都会从一个坐标移动到下一个坐标,我想在我单击的每个位置创建图像的副本,我该如何实现?

您必须创建一个模型(如 ListModel (,其中包含用作MapQuickItem委托的MapItemView

ListModel {
    id: markermodel
    dynamicRoles: true
}
Map {
    id: map
    anchors.fill: parent
    plugin: Plugin {
        name: "osm"
    }
    center: QtPositioning.coordinate(59.91, 10.75)
    zoomLevel: 10
    MapItemView{
        model: markermodel
        delegate: MapQuickItem {
            coordinate: model.position
            sourceItem: Image {
                width: 50
                height: 50
                source: "trans.png"
            }
        }
    }
    MouseArea{
        anchors.fill: parent
        onClicked: {
            var coord = map.toCoordinate(Qt.point(mouse.x,mouse.y));
            markermodel.append({"position": coord})
        }
    }
}

最新更新