QtQuick:在地图上拖动 MapQuickItem

QtQuick: dragging MapQuickItem on a Map

我想实现自拖MapQuickItem。简单例子:

MapQuickItem {
    id: markerItem

    sourceItem: Rectangle {
        id: sourceRect
        color: "red"
        width: 20
        height: 20
        x: 0
        y: 0

        MouseArea {
            drag.target: markerItem
            cursorShape: drag.active ? Qt.ClosedHandCursor : Qt.OpenHandCursor
            anchors.fill: parent
        }
    }
    Drag.active: true
}

重点是,如果我快速拖动,光标一离开标记就会中断拖动。有没有办法让它正常工作?

我找到了解决方法:使用单独的可拖动 QQuickItem 和锚点 MapQuickItem:

MapQuickItem {
    id: anchor
    sourceItem: Item {}
}

Rectangle {
    id: handle

    property bool dragged: mouseArea.drag.active

    color: "red"
    width: 20
    height: 20
    x: anchor.x - width
    y: anchor.y - height

    MouseArea {
        id: mouseArea
        enabled: draggable
        drag.target: handle
        drag.threshold: 0
        anchors.fill: parent
        cursorShape: dragged ? Qt.ClosedHandCursor : Qt.OpenHandCursor
    }

    Connections {
        target: anchor
        onXChanged: if (!dragged) x = anchor.x - width
        onYChanged: if (!dragged) y = anchor.y - height
    }

    onXChanged: if (dragged) anchor.x = x + width
    onYChanged: if (dragged) anchor.y = y + height

    Drag.active: true
}

动态填充不是很方便QML Map,但可以做到