android - QML 映射 : get coordinates when tap the screen

标签 android c++ qt maps qml

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

主.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));

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

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

    }

如何将 map 上的标记定位在发生点击的坐标处?谢谢。

最佳答案

只需在onPressed 处理程序中设置marker坐标。类似于以下内容:

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

关于android - QML 映射 : get coordinates when tap the screen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34637133/

相关文章:

java - 输入长文本后,具有自定义 View 的 AlertDialog 消失在软键盘后面

c++ - 在 QT Creator 中使用 makefile

c++ - 如何杀死或终止 boost 线程

qt - 任何编译之前的 qmake 预构建步骤

android - 在 Android SDK 中选择 Wifi Direct channel /频段?

android - 中文语言环境值-zh_CN 在 Eclipse 上报错

c++ - 将 RAII 结构作为二维数组传递给函数

c++ - 在 C++ 中从 8 位 ASCII 字符创建 7 位 ASCII 文本字符串

c++ - C++ 中的私有(private)构造函数

java - APICall 响应解析数据失败