在qml中,当两个组件被放置在地图上的同一坐标上时,如何同时拖动它们到地图上?

问题描述 投票:0回答:1

我想拖动两个MapQuickItem,这个MapQuickItem是在组件中声明的,并从地图中获取实时坐标。MapQuickItem组件是一个红色和绿色的矩形。我想同时拖动两个处于同一坐标的矩形。如何在地图上以相同的坐标同时拖动两个组件。

    Map {
    id: map
    anchors.fill: parent
    activeMapType: supportedMapTypes[1];
    zoomLevel: 18
    plugin: hereMaps
    center: QtPositioning.coordinate(19.997454, 73.789803)

    MapItemView {
        id: markerItem
        model: [
            { id: "marker1", color: "red" },
            { id: "marker2", color: "green" },
         ]
        delegate: mapMarkerComponent
    }

    Component {
        id : mapMarkerComponent

        MapQuickItem {
            id: mapMarker
            coordinate: QtPositioning.coordinate(19.997454, 73.789803)

            sourceItem: Rectangle {

                id: handle
                color: modelData.color
                width: 40
                height: 40

                MouseArea {
                    drag.target: parent
                    anchors.fill: parent
                }

                onXChanged: {
                    mapMarker.x += x
                }

                onYChanged: {
                    mapMarker.y += y
                }
            }
        }
    }
}
qt drag-and-drop qml components maps
1个回答
0
投票

这里的代码对我来说是可行的,即拖动 marker2 隐性拖动 marker1.

Map {
    anchors.fill: parent
    plugin: mapPlugin
    center: QtPositioning.coordinate(59.91, 10.75) // Oslo
    zoomLevel: 14

    MapQuickItem {
        id: marker1
        anchorPoint.x: 15
        anchorPoint.y: 15
        sourceItem: Rectangle {
            id: rect1
            width: 30
            height: 30
            radius: 15
            color: "#8000FF00"
        }
        coordinate: QtPositioning.coordinate(marker2.coordinate.latitude + 0.005, marker2.coordinate.longitude)
    }

    MapQuickItem {
        id: marker2
        anchorPoint.x: 10
        anchorPoint.y: 10
        sourceItem: Rectangle {
            id: rect2
            width: 20
            height: 20
            radius: 10
            color: "#800000FF"
            MouseArea {
                drag.target: parent
                anchors.fill: parent
            }
            onXChanged: marker2.x += x;
            onYChanged: marker2.y += y;
        }
        coordinate: QtPositioning.coordinate(59.91, 10.75)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.