0

我想拖动一个在组件内部声明的 MapQuickItem,并从地图中获取实时坐标。运行代码时,我收到这样的错误“qrc:/main.qml:15: ReferenceError: mouseArea is not defined”。如何在组件外访问mouseAea?或者在哪里声明拖动的属性来访问鼠标区域?

Window {
id: window
width: 800
height: 800
visible: true

property bool dragged: mouseArea.drag.active

Plugin
{
    id: hereMaps
    name: "here"
    PluginParameter { name: "here.app_id"; value: "oBB4FivcP23m2UZQCj8K" }
    PluginParameter { name: "here.token"; value: "P-D8XRRGeVt0YphUuOImeA" }
}

Map {
    id: mapOfWorld
    anchors.centerIn: parent;
    anchors.fill: parent
    activeMapType: supportedMapTypes[1];
    zoomLevel: 18
    plugin: hereMaps
    center: QtPositioning.coordinate(19.997454, 73.789803)
    MouseArea{
        id : mapAreaClick
        height: mapOfWorld.height
        width: mapOfWorld.width
        hoverEnabled: true
        anchors.fill: mapOfWorld
        preventStealing : true
        propagateComposedEvents : true
        anchors.centerIn: mapOfWorld
    }

       Component {    // here error occurs
        id : test 
    MapQuickItem
        {
            id: anchor
           coordinate: QtPositioning.coordinate(19.997454, 73.789803)
            sourceItem: Item {
                Rectangle {
                    id: handle
                    color: "red"
                    width: 40
                    height: 40
                    radius: 40
                    x: anchor.x - width
                    y: anchor.y - height
                    Drag.active: true
                    MouseArea {
                        id: mouseArea
                        drag.target: handle
                        drag.threshold: 0
                        anchors.fill: parent
                    }

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

                    onXChanged: {
                        console.log("X:"+x)
                        var cordinate = mapOfWorld.toCoordinate((Qt.point((x),(y))));
                        console.log("onXChanged :" , cordinate)
                        if (dragged) anchor.x = x + width
                    }
                    onYChanged:{
                        console.log("Y:"+y)
                        var cordinate = mapOfWorld.toCoordinate((Qt.point((x),(y))));
                        console.log("onYChanged : ", cordinate)
                        if (dragged) anchor.y = y + height
                    }
                }
            }
       }
    }
   }
   }
4

2 回答 2

1

mouseArea未定义,因为没有创建对象。所以你必须从test组件创建对象var anchorItem = test.createObject(),然后将它添加到你的mapOfWorld.addMapItem(anchorItem)

Window {
    id: window
    ...        
    //property bool dragged: mouseArea.drag.active
    Map{
       id: mapOfWorld
       ...
        Component {    // here error occurs
            id : test 
            MapQuickItem
            {
                id: anchor
                //moved here
                property bool dragged: mouseArea.drag.active 
                ...
        }
        //create our anchor object///
        Component.onCompleted : {
            var mapAnchor = test.createObject()
            mapOfWorld.addMapItem(mapAnchor)

        }
    }
}
于 2020-04-17T17:37:29.763 回答
1

当我看到这个问题时,我以为您正在尝试重新发明轮子。我以前从未在 Qt 中使用过 Maps,但我确信必须有最简单的方法来实现您的目标。所以在这里你有一点不同的解决方案。更简单,在我看来:

Window {
    ...
    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" },
                { id: "marker3", color: "blue" }
            ]
            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
                    }
                }

                onCoordinateChanged: {
                    console.log(modelData.id + ":" + coordinate);
                }
            }
        }
    }
}

我认为那里的一切都说明了自己:)

于 2020-04-17T19:11:12.117 回答