简体   繁体   English

向 Qt QML Maps 添加标记/位置?

[英]Adding markers/places to Qt QML Maps?

Good day !再会 ! I am designing a Qt-based GUI platform that displays a map and allows users to add markers/pins on top of the map.我正在设计一个基于 Qt 的 GUI 平台,它显示地图并允许用户在地图顶部添加标记/图钉

I am rendering the map in a QtQuickWidget using the following QML:我正在使用以下 QML 在 QtQuickWidget 中渲染地图:

Plugin {
    id: mapPlugin
    name: "osm"
}

I want to allow the user to add an interactive pin on the map using a button on a form.我想允许用户使用表单上的按钮在地图上添加交互式图钉 The user can press and hold a point on the map which will open the form, where the user can name the place and press OK .用户可以按住地图上的一个点,这将打开表单,用户可以在其中命名地点并按OK

Example of interactive pin互动图钉示例

Example of what I want to achieve: https://webmshare.com/play/5EXV8我想要实现的示例: https ://webmshare.com/play/5EXV8


  1. I have tried using QQmlComponent and QQuickView but I was unsuccessful [ http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html]我试过使用 QQmlComponent 和 QQuickView 但我没有成功 [ http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html]

  2. Another way is to add objects within QML itself using MapItems but this is very unintuitive.另一种方法是使用MapItems在 QML 本身内添加对象,但这是非常不直观的。 This is what my map.qml looks like: https://gist.github.com/blackvitriol/7941688d6362162888630a28c79f8cd9这就是我的 map.qml 的样子: https ://gist.github.com/blackvitriol/7941688d6362162888630a28c79f8cd9

Project Structure: https://imgur.com/a/P8YAS项目结构: 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?有人能告诉我如何让用户在地图上按住左键单击,然后向该点添加标记吗?

A possible solution is to use MapItemView and create a model that stores the coordinates:一种可能的解决方案是使用MapItemView并创建一个存储坐标的模型:

markermodel.h标记模型.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主.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主.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();
}

在此处输入图像描述

The complete example can be found in the following link .完整示例可在以下链接中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM