在 QML qt 中控制 OSM 映射 [英] Control OSM map in QML qt

查看:21
本文介绍了在 QML qt 中控制 OSM 映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Qt 中控制地图,但是我一直遇到以下错误:

I am trying to control the map in Qt however I keep ending up with the following error:

QGeoTileRequestManager: Failed to fetch tile (291271,152514,19) 5 times, giving up. Last error message was: 'Permission denied'

我在 C++ 中有解析消息并计算位置的函数:

I have functions in C++ that parse the messages and calculates the position:

Map.qml

import QtQuick 2.12
import QtQuick.Window 2.14
import QtQuick.Controls 2.15
import QtLocation 5.6
import QtPositioning 5.6
import QtQuick.Controls.Material 2.12
import QtQuick.Layouts 1.12
import Qt.labs.location 1.0

Page {
    id: mainWindow
    visible: true

    function addMarker(latitude, longitude)
    {
        var Component = Qt.createComponent("qrc:/Marker.qml")
        var item = Component.createObject(window, {
                                              coordinate: QtPositioning.coordinate(latitude, longitude)
                                          })
        map.addMapItem(item)
    }

    Map {
        id: map
        width: mainWindow.width
        height: mainWindow.height
        plugin: mapPlugin
        center: QtPositioning.coordinate(59.91, 10.75)
        Component.onCompleted: addMarker(59.91, 10.75)
        zoomLevel: 60

    }

    Plugin {
        id: mapPlugin
        name: "osm" // "mapboxgl", "esri", ...
        // specify plugin parameters if necessary
        PluginParameter {
            name: "osm.mapping.providersrepository.disabled"
            value: "true"
        }
        PluginParameter {
            name: "osm.mapping.providersrepository.address"
            value: "http://maps-redirect.qt.io/osm/5.6/"
        }
    }
}

通过Q_PROPERTY设置坐标:

Setting the coordinates is done through Q_PROPERTY:

#include <QObject>

class Data : public QObject
{
    Q_OBJECT;
    Q_PROPERTY(double gnss_log READ gnss_long WRITE set_gnss_long NOTIFY gnss_long_changed);
    Q_PROPERTY(double gnss_lat READ gnss_lat WRITE set_gnss_lat NOTIFY gnss_lat_changed);
public: signals:

    void gnss_long_changed();
    void gnss_lat_changed();

public slots:

    void set_gnss_long(double);
    void set_gnss_lat(double);

public:
    Data();

    double gnss_long();
    double gnss_lat();

private:
    double m_gnss_long;
    double m_gnss_lat;
};


void Data::set_gnss_long(double curr_long)
{
    // Checks whether updated baud rate changed
    if (curr_long == m_gnss_long)
        return;

    m_gnss_long = curr_long;
    qDebug() << m_gnss_long;
    //Emits signal indicating change
    emit gnss_long_changed();
}

double Data::gnss_long()
{
    return m_gnss_long;
}

当我运行它时,我得到一个空白屏幕,上面提到了一堆错误.

I when I run this, I get a blank screen with a bunch of the errors mentioned above.

推荐答案

由于 OP 没有提供 MRE,我的答案只会显示一个演示.逻辑是创建一个暴露位置的QProperty,然后必须做一个绑定:

Since the OP has not provided an MRE my answer will only show a demo. The logic is to create a QProperty that exposes the position, then a binding must be done:

#include <QGeoCoordinate>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTimer>

#include <random>

class Data: public QObject{
    Q_OBJECT
    Q_PROPERTY(QGeoCoordinate gnssPosition READ gnssPosition WRITE setGnssPosition NOTIFY gnssPositionChanged)
public:
    const QGeoCoordinate &gnssPosition() const{
        return m_gnssPosition;
    }
    void setGnssLatitude(qreal latitude){
        QGeoCoordinate coordinate(latitude, m_gnssPosition.longitude());
        setGnssPosition(coordinate);
    }
    void setGnssLongitude(qreal longitude){
        QGeoCoordinate coordinate(m_gnssPosition.latitude(), longitude);
        setGnssPosition(coordinate);
    }
    void setGnssPosition(const QGeoCoordinate &newGnssPosition){
        if (m_gnssPosition == newGnssPosition)
            return;
        m_gnssPosition = newGnssPosition;
        emit gnssPositionChanged();
    }
signals:
    void gnssPositionChanged();
private:
    QGeoCoordinate m_gnssPosition;
};

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    Data data_out;
    data_out.setGnssPosition(QGeoCoordinate(59.91273, 10.74609));

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("data_out", &data_out);
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    QTimer timer;
    timer.setInterval(1000);
    QObject::connect(&timer, &QTimer::timeout, &data_out, [&data_out](){
        std::random_device rd;
        std::mt19937 e2(rd());
        std::uniform_real_distribution<> dist(-.05, .05);

        QGeoCoordinate coord = data_out.gnssPosition();
        coord.setLatitude(coord.latitude() + dist(e2));
        coord.setLongitude(coord.longitude() + dist(e2));
        data_out.setGnssPosition(coord);
        // qDebug() << data_out.gnssPosition();
    });
    timer.start();

    return app.exec();
}

#include "main.moc"

import QtQuick 2.15
import QtQuick.Window 2.15
import QtLocation 5.15
import QtPositioning 5.15

Window {
    id: mainWindow
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    property Component markerProvider: MapQuickItem {
        anchorPoint.x: rect.width / 2
        anchorPoint.y: rect.height / 2
        sourceItem: Rectangle{
            id: rect
            width: 40
            height: 40
            color: "salmon"
        }
    }

    function addMarker(coordinate){
        var marker = markerProvider.createObject()
        console.log(marker)
        marker.coordinate = coordinate
        map.addMapItem(marker)
    }

    Map {
        id: map
        width: mainWindow.width
        height: mainWindow.height
        plugin: mapPlugin
        center: data_out.gnssPosition
        zoomLevel: 12

    }

    Plugin {
        id: mapPlugin
        name: "osm" // "mapboxgl", "esri", ...
        // specify plugin parameters if necessary
        PluginParameter {
            name: "osm.mapping.providersrepository.disabled"
            value: "true"
        }
        PluginParameter {
            name: "osm.mapping.providersrepository.address"
            value: "http://maps-redirect.qt.io/osm/5.6/"
        }
    }
    Connections{
        target: data_out
        function onGnssPositionChanged(){
            addMarker(data_out.gnssPosition)
        }
    }

}

这篇关于在 QML qt 中控制 OSM 映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆