首页 文章

在Qt QML Maps中添加标记/位置?

提问于
浏览
1

美好的一天 !我正在设计一个显示 Map 的 Qt-based GUI platform ,并允许用户在 Map 上添加标记/图钉 .

我使用以下QML在QtQuickWidget中渲染 Map :

Plugin {
    id: mapPlugin
    name: "osm"
}

我想让用户使用表单上的按钮 add an interactive pin on the map . 用户可以按住 Map 上的一个点,该点将打开表单,用户可以在其中命名该地点并按 OK .

Example of interactive pin

我想要实现的例子:https://webmshare.com/play/5EXV8


项目结构:https://imgur.com/a/P8YAS

Can someone tell me how to allow the user to press and hold left click on the map, then add a marker to that point ?

1 回答

  • 0

    一种可能的解决方案是使用 MapItemView 并创建一个存储坐标的模型:

    markermodel.h

    #ifndef MARKERMODEL_H
    #define MARKERMODEL_H
    
    #include <QAbstractListModel>
    #include <QGeoCoordinate>
    
    class MarkerModel : public QAbstractListModel
    {
        Q_OBJECT
    
    public:
        using QAbstractListModel::QAbstractListModel;
        enum MarkerRoles{positionRole = Qt::UserRole + 1};
    
        Q_INVOKABLE void addMarker(const QGeoCoordinate &coordinate){
            beginInsertRows(QModelIndex(), rowCount(), rowCount());
            m_coordinates.append(coordinate);
            endInsertRows();
        }
    
        int rowCount(const QModelIndex &parent = QModelIndex()) const override{
            Q_UNUSED(parent)
            return m_coordinates.count();
        }
    
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override{
            if (index.row() < 0 || index.row() >= m_coordinates.count())
                return QVariant();
            if(role== MarkerModel::positionRole)
                return QVariant::fromValue(m_coordinates[index.row()]);
            return QVariant();
        }
    
        QHash<int, QByteArray> roleNames() const{
            QHash<int, QByteArray> roles;
            roles[positionRole] = "position";
            return roles;
        }
    
    private:
        QList<QGeoCoordinate> m_coordinates;
    };
    
    #endif // MARKERMODEL_H
    

    main.qml

    import QtQuick 2.0
    import QtLocation 5.6
    import QtPositioning 5.6
    import QtQuick.Window 2.0
    
    Rectangle {
        width: Screen.width
        height: Screen.height
        visible: true
    
        Plugin {
            id: mapPlugin
            name: "osm"
        }
    
        Map {
            id: mapview
            anchors.fill: parent
            plugin: mapPlugin
            center: QtPositioning.coordinate(59.91, 10.75)
            zoomLevel: 14
    
            MapItemView{
                model: markerModel
                delegate: mapcomponent
            }
        }
    
        Component {
            id: mapcomponent
            MapQuickItem {
                id: marker
                anchorPoint.x: image.width/4
                anchorPoint.y: image.height
                coordinate: position
    
                sourceItem: Image {
                    id: image
                    source: "http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_red.png"
                }
            }
        }
    
        MouseArea {
            anchors.fill: parent
    
            onPressAndHold:  {
                var coordinate = mapview.toCoordinate(Qt.point(mouse.x,mouse.y))
                markerModel.addMarker(coordinate)
            }
        }
    }
    

    main.cpp

    #include "markermodel.h"
    
    #include <QApplication>
    #include <QQuickWidget>
    #include <QQmlContext>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QQuickWidget w;
        MarkerModel model;
        w.rootContext()->setContextProperty("markerModel", &model);
        w.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
        w.show();
    
        return a.exec();
    }
    

    enter image description here

    完整的示例可以在以下link中找到 .

相关问题