Как перетащить компонент на карту в мышиной области и получить координаты карты в реальном времени - PullRequest
0 голосов
/ 17 апреля 2020

Я хочу перетащить MapQuickItem, который объявлен внутри компонента и получает координаты в реальном времени с карты. При запуске кода я получаю сообщение об ошибке "qr c: / main.qml: 15: ReferenceError: mouseArea не определено", как это. Как получить доступ к mouseAea вне компонента? Или где объявить перетаскиваемое свойство для получения доступа к mousearea?

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
                    }
                }
            }
       }
    }
   }
   }

Ответы [ 2 ]

1 голос
/ 17 апреля 2020

Когда я увидел этот вопрос, я подумал, что вы пытаетесь изобрести колесо. Я никогда раньше не работал с Maps в Qt, но AFAIK Qt Я был уверен, что должен быть самый простой способ достичь своей цели. Так что здесь у вас есть немного другое решение. Проще, на мой взгляд:

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);
                }
            }
        }
    }
}

Я думаю, что все там само собой объясняется:)

1 голос
/ 17 апреля 2020

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)

        }
    }
}
...