I'm making project using Qt Creator (Community) 5.5.1 with support of QML. I have this code:
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));
So when I tap the screen, the coordinates of this place on the map are shown on console. But I don't know how I can use these coordinates to position the marker on the screen where the clicked occurred. Here is the marker code:
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
}
What can I do to position the marker on map at the coordinates where the click occurred? Thanks.
Just set the coordinate
of marker
in the onPressed
handler. Something like the following:
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))
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With