1
我使用QML Creator(Community)5.5.1支持QML項目。 我有這樣的代碼:QML-maps:點擊屏幕時獲取座標
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
}
我該如何將標記定位在發生點擊的座標上的地圖上?謝謝。
謝謝!但是如果我在地圖上沒有任何標記,它會成爲新標記?我能怎麼做? – Khan
@Khan,你的目標是擁有一個標記嗎?你是否希望它只在點擊發生時出現?如果是這樣,那麼如何將標記的「visible」設置爲false,然後在'onPressed'手柄中將其設置爲true。 – nfranklin
如果您想「創建」每個新點擊的新標記,請查看[動態對象創建文檔](http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html)..或更好的是,使用模型(添加點擊位置的地方)和[MapItemView](http://doc.qt.io/qt-5/qml-qtlocation-mapitemview.htm) – nfranklin