我正在使用支持 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/