QML-maps:点击屏幕时获取坐标
QML-maps: get coordinates when tap the screen
我正在使用支持 QML 的 Qt Creator(社区)5.5.1 制作项目。
我有这个代码:
main.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));
所以当我点击屏幕时,这个地方在地图上的坐标会显示在控制台上。但我不知道如何使用这些坐标将标记定位在发生点击的屏幕上。这是标记代码:
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
}
如何将地图上的标记定位在发生点击的坐标处?谢谢
只需在 onPressed
处理程序中设置 marker
的 coordinate
。类似于以下内容:
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))
}
}
}
我正在使用支持 QML 的 Qt Creator(社区)5.5.1 制作项目。 我有这个代码:
main.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));
所以当我点击屏幕时,这个地方在地图上的坐标会显示在控制台上。但我不知道如何使用这些坐标将标记定位在发生点击的屏幕上。这是标记代码:
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
}
如何将地图上的标记定位在发生点击的坐标处?谢谢
只需在 onPressed
处理程序中设置 marker
的 coordinate
。类似于以下内容:
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))
}
}
}