Ivan Triumphov
Ivan Triumphov

Reputation: 155

How do I move a geographic map during a pressAndHold event in a Qt application?

There is a program that transmits latitude and longitude in "Line Edit". But at the same time the opportunity to move the card is lost. How do I make the map move behind the mouse during the pressAndHold event?

places_map.qml:

import QtQuick 2.0
import QtLocation 5.6
import QtPositioning 5.6

Rectangle {
    id: rect
    Plugin {
        id: mapPlugin
        name: "osm" // "mapboxgl", "esri", ...
        // specify plugin parameters if necessary
        // PluginParameter {
        //     name:
        //     value:
        // }
    }


    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(59.91, 10.75) // Oslo
        zoomLevel: 14
    }

    MouseArea{
        anchors.fill: parent
        onClicked:  lineEdit.text = ""+ map.toCoordinate(Qt.point(mouse.x,mouse.y))
    }
}

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QQmlContext>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->quickWidget->rootContext()->setContextProperty("lineEdit", ui->lineEdit);
}

MainWindow::~MainWindow()
{
    delete ui;
}

Upvotes: 3

Views: 618

Answers (1)

eyllanesc
eyllanesc

Reputation: 244212

One way to move the map is to use gesture as I show below:

Map {
    [...]
    gesture.enabled: true
    gesture.acceptedGestures: MapGestureArea.PanGesture
}

But when using the MouseArea it blocks these actions, so we can create the same effect using onPressed and onPositionChanged:

MouseArea{
    anchors.fill: parent

    property int lastX : -1
    property int lastY : -1

    onPressed : {
        lastX = mouse.x
        lastY = mouse.y
    }

    onPositionChanged: {
        map.pan(lastX-mouse.x, lastY-mouse.y)
        lastX = mouse.x
        lastY = mouse.y
    }
    onClicked:  lineEdit.text = ""+ map.toCoordinate(Qt.point(mouse.x,mouse.y))
}

Upvotes: 2

Related Questions