QML-maps:点击屏幕时获取坐标

Kha*_*han 5 c++ maps qt android qml

我正在使用 Qt Creator(社区)5.5.1 制作项目并支持 QML。我有这个代码:

主.qml:

MouseArea
        {   anchors.fill: parent
            onPressed: console.log('latitude = '+ (map.toCoordinate(Qt.point(mouse.x,mouse.y)).latitude),
                                   'longitude = '+ (map.toCoordinate(Qt.point(mouse.x,mouse.y)).longitude));
Run Code Online (Sandbox Code Playgroud)

所以当我点击屏幕时,地图上这个地方的坐标会显示在控制台上。但我不知道如何使用这些坐标在发生点击的屏幕上定位标记。这是标记代码:

MapQuickItem {
        id:marker
        coordinate:  QtPositioning.coordinate(******, ******);//stars are the coordinates
        sourceItem: Image{
            id: image
            source: "marker2.png"

        }
        anchorPoint.x: image.width / 2
        anchorPoint.y: image.height

    }
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能将标记定位在地图上发生点击的坐标处?谢谢。

nfr*_*lin 5

只需在处理程序中设置coordinateof即可。像下面这样:markeronPressed

import QtQuick 2.0
import QtLocation 5.5

Map {
    id: map
    plugin: Plugin {name: "osm"}
    zoomLevel: (maximumZoomLevel - minimumZoomLevel)/2
    center {
        // The Qt Company in Oslo
        latitude: 59.9485
        longitude: 10.7686
    }

    MapQuickItem {
        id:marker
        sourceItem: Image{
            id: image
            source: "marker2.png"

        }
        coordinate: map.center
        anchorPoint.x: image.width / 2
        anchorPoint.y: image.height / 2
    }

    MouseArea {
        anchors.fill: parent
        onPressed: {
            marker.coordinate = map.toCoordinate(Qt.point(mouse.x,mouse.y))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)