Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML-maps: get coordinates when tap the screen

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.

like image 962
Khan Avatar asked Sep 25 '22 18:09

Khan


1 Answers

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))
        }
    }
}
like image 190
nfranklin Avatar answered Sep 29 '22 07:09

nfranklin