2017-11-20 1 views
0

지도에 Qt/QML로 많은 수의 점을 표시하고 싶습니다. 이 요점은 .txt 파일에 있습니다.QML로 파일의 위치를 ​​표시하는 방법

사용하려는 프로세스를 찾고 있지만 불행히도 서버 (osm, google ...)의 위치를 ​​표시하는 플러그인을 통해서만 쿼리 메소드를 찾을 수 있습니다. 나는 그것을 사용할 수 없다.이 점들은 매우 구체적이다.

이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

또한 특정 레이어에서 이러한 점을 표시하려면 "osm plugin"외에도 "plugin itemsoverlay"를 사용해야합니까?

도움 주셔서 감사합니다.

이제 코드로 다시 시도하십시오.

다음은 주요 통화 당

#include <QGuiApplication> 
#include <QQmlApplicationEngine> 
#include <QQmlContext> 
#include <QObject> 
#include <QGeoCoordinate> 
#include <QDebug> 

class NavaidsPoint: public QObject 
{ 
    Q_OBJECT 
    Q_PROPERTY(QGeoCoordinate position READ position WRITE setPosition NOTIFY positionChanged) 

public: 
    NavaidsPoint(QString code, double latitude, double longitude, QString country = "") 
    { 
     m_code = code; 
     m_latitude = latitude; 
     m_longitude = longitude; 
     m_country = country; 
     m_position.setLatitude(latitude); 
     m_position.setLongitude(longitude); 
    } 

    void setPosition(const QGeoCoordinate &c) { //Affectation des nouvelles coordonnées de position 
     if (m_position == c) 
      return; 

     m_position = c; 
     emit positionChanged(); //Emission du signal de changement de position 
    } 

    QGeoCoordinate position() const 
    { 
     return m_position; //Lecture des coordonnées de position 
    } 

    Q_INVOKABLE QString oaciCode() const { 
     return m_code; 
    } 

    Q_INVOKABLE QString countryCode() const { 
     return m_country; 
    } 

signals: 
    void positionChanged(); 

private: 
    QGeoCoordinate m_position; 
    double m_latitude; 
    double m_longitude; 
    double m_altitude; 
    QString m_code; 
    QString m_country; 
}; 




int main(int argc, char *argv[]) 
{ 
    QGuiApplication app(argc, argv); 

    NavaidsPoint oslo("Oslo", 59.9154, 10.7425, "NG"); 

    QQmlApplicationEngine engine; 
    engine.rootContext()->setContextProperty("oslo", &oslo); 
    engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); 
    if (engine.rootObjects().isEmpty()) 
     return -1; 

    return app.exec(); 
} 

#include "main.moc" 

그리고

import QtQuick 2.6 
import QtQuick.Window 2.2 
import QtPositioning 5.5 
import QtLocation 5.6 

Window { 
    width: 700 
    height: 500 
    visible: true 
    title: qsTr("Test implantation coordonnées") 

    property variant topLeftEurope: QtPositioning.coordinate(60.5, 0.0) 
    property variant bottomRightEurope: QtPositioning.coordinate(51.0, 14.0) 
    property variant viewOfEurope: 
      QtPositioning.rectangle(topLeftEurope, bottomRightEurope) 

    Map { 
     id: mapOfEurope 
     anchors.centerIn: parent; 
     anchors.fill: parent 
     plugin: Plugin { 
      name: "osm" 
     } 

     MapCircle { 
      center: oslo.position 
      radius: 5000.0 
      color: 'green' 
      border.width: 3 
      MouseArea { 
       anchors.fill: parent 
       onDoubleClicked: { 
        console.log("Doubleclick on " + oslo.oaciCode()) 
       } 
       onClicked: { 
        console.log("Point : " + oslo.oaciCode() + " " + oslo.position + " " + oslo.countryCode()) 
       } 
      } 
     } 
    visibleRegion: viewOfEurope 
    } 
} 

모든 것이 독특한 점 오슬로와 함께 잘 작동 main.qml입니다. 이제 수천 점을 배치해야합니다. 이 구조체는 각각의 NavaidsPoint를 구현하고 각각에 대해 setContextProperty를 다시 구현해야하기 때문에 그렇게 작동하지 않습니다. 마찬가지로, main.QML에서 원은 개체와 연결되어 있습니다 :

center : oslo.position 

및 추가

, 나는 oaciCode 및 countryCode와를이 nead. 따라서 코드의이 부분은 단일 객체와 관련이없는 일반 사항이어야합니다.

그래서이 문제를 해결하는 가장 좋은 방법은 무엇입니까?

이 정밀도로 SO 범위에 있기를 바랍니다.

도움 주셔서 다시 한 번 감사드립니다.

+0

은이 .txt를 제시해주십시오 – eyllanesc

+0

ROBSO, 21.75000, -107.12556, MM ROBSU, 49.83348, -64.04158, CY ROBTE, 45.40642, -91., K5 ROBUC, 41.67892, -71.58512, K6 ROBUD, 40.64376 - 118.70789, K2 ROBUE, 62.45198, -160.23723, PA ROBUM, 69.43252,15.91136, EN ROBUN, 74.83083,74.18083, UL ROBUR, 61.88620, -6.57648, EK ROBUS, 55.10944,11.71972, EK ROBUT, 46.00639, 42.70333, UR ROBVE, 36.30598, -99.19448, K4 ROBVI, 52.57165,3.77645, EH ROBVO, 51.68444, -8.19925, EI ROBVY, 38.78295, -10 6.10674, K2 ROBYE, 35.72012, -89.81856, K7 ROBYN, 19.76366, -156.11584, PH ... 여기에 스 니펫과 일부 수천 행이 추가됩니다. – kontiki

+0

난 당신이 어떤 어려움이 있다면 당신이 가지고있는 오류를 자세히 설명하는 질문을 게시하고 시도한 코드를 보여주는 당신의 문제를 해결하기 위해 노력하는 것이 좋습니다. 그런 다음 우리는 당신을 도우려고 노력할 것입니다. SO는 코딩 서비스가 아닙니다. – eyllanesc

답변

0

많은 요소를로드하려는 경우 MapItemView를 사용하는 것이 좋습니다. 데이터를 제공하는 모델이 필요하며 적절한 대리인을 지정해야합니다.

모델은 우리가 그 다음이 될 것이다,이 경우에는 반드시 QObject에서 상속하지 않는 클래스를 생성하기위한, QML에 속성을 노출하는 역할을 사용하는 QAbstractListModel를 기반으로 작성됩니다.후

#ifndef NAVAIDSMODEL_H 
#define NAVAIDSMODEL_H 

#include "navaidspoint.h" 

#include <QAbstractListModel> 
#include <QFile> 
#include <QTextStream> 

#include <QDebug> 

class NavaidsModel : public QAbstractListModel 
{ 
    Q_OBJECT 
public: 
    NavaidsModel(QObject *parent = Q_NULLPTR):QAbstractListModel(parent){ 
    } 
    enum NavaidsRoles { 
     PositionRole = Qt::UserRole + 1, 
     OACICodeRole, 
     CountryCodeRole 
    }; 

    void readFromCSV(const QString &filename){ 
     QFile file(filename); 
     if(!file.open(QFile::ReadOnly | QFile::Text)) 
      return; 
     QTextStream in(&file); 
     while (!in.atEnd()) { 
      QString line = in.readLine(); 
      QStringList elements = line.split(","); 
      if(elements.count()==4){ 
       QString code = elements[0]; 
       double latitude = elements[1].toDouble(); 
       double longitude = elements[2].toDouble(); 
       QString country = elements[3]; 
       NavaidsPoint p(code, latitude, longitude, country); 
       addNavaidsPoint(p); 
      } 
     } 
    } 

    void addNavaidsPoint(const NavaidsPoint &point){ 
     beginInsertRows(QModelIndex(), rowCount(), rowCount()); 
     mPoints << point; 
     endInsertRows(); 
    } 

    int rowCount(const QModelIndex & parent = QModelIndex()) const{ 
     Q_UNUSED(parent) 
     return mPoints.count(); 
    } 

    QVariant data(const QModelIndex & index, int role=Qt::DisplayRole) const { 
     if (index.row() < 0 || index.row() >= mPoints.count()) 
      return QVariant(); 

     const NavaidsPoint &point = mPoints[index.row()]; 
     if (role == PositionRole) 
      return QVariant::fromValue(point.position()); 
     else if (role == OACICodeRole) 
      return point.oaciCode(); 
     else if (role == CountryCodeRole) 
      return point.countryCode(); 
     return QVariant(); 
    } 

protected: 
    QHash<int, QByteArray> roleNames() const { 
     QHash<int, QByteArray> roles; 
     roles[PositionRole] = "position"; 
     roles[OACICodeRole] = "oaci"; 
     roles[CountryCodeRole] = "country"; 
     return roles; 
    } 
private: 
    QList<NavaidsPoint> mPoints; 
}; 

#endif // NAVAIDSMODEL_H 

을 따를

#ifndef NAVAIDSPOINT_H 
#define NAVAIDSPOINT_H 

#include <QGeoCoordinate> 
#include <QString> 

class NavaidsPoint 
{ 
public: 
    NavaidsPoint(QString code, double latitude, double longitude, QString country = ""){ 
     m_code = code; 
     m_country = country; 
     m_position.setLatitude(latitude); 
     m_position.setLongitude(longitude); 
     m_position.setAltitude(0.0); 
    } 

    void setPosition(const QGeoCoordinate &c) { //Affectation des nouvelles coordonnées de position 
     m_position = c; 
    } 

    QGeoCoordinate position() const{ 
     return m_position; //Lecture des coordonnées de position 
    } 

    QString oaciCode() const { 
     return m_code; 
    } 

    QString countryCode() const { 
     return m_country; 
    } 

private: 
    QGeoCoordinate m_position; 
    QString m_code; 
    QString m_country; 
}; 


#endif // NAVAIDSPOINT_H 

QAbstractListModel 당신이 방법 rowCount, dataroleNames을 구현해야합니다, 또한 당신이 변화의 모델을 통보해야합니다 추가 방법을 구현 한 예입니다 모델이 생성되고 모델이 추가되고 QML에 노출됩니다 :

#include "navaidsmodel.h" 

#include <QGuiApplication> 
#include <QQmlApplicationEngine> 
#include <QQmlContext> 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); 
    QGuiApplication app(argc, argv); 

    NavaidsModel model; 
    model.readFromCSV("/home/eyllanesc/data.csv"); //from file 

    model.addNavaidsPoint(NavaidsPoint("Oslo", 59.9154, 10.7425, "NG")); // add new point 

    QQmlApplicationEngine engine; 
    engine.rootContext()->setContextProperty("navaidsModel", &model); 
    engine.load(QUrl(QLatin1String("qrc:/main.qml"))); 
    if (engine.rootObjects().isEmpty()) 
     return -1; 

    return app.exec(); 
} 

및 마지막에, MapItemView 적절한 모델과 위임에 사용되는 다음 link에서

import QtQuick 2.6 
import QtQuick.Window 2.2 
import QtPositioning 5.5 
import QtLocation 5.6 

Window { 
    width: 700 
    height: 500 
    visible: true 
    title: qsTr("Test implantation coordonnées") 

    property variant topLeftEurope: QtPositioning.coordinate(60.5, 0.0) 
    property variant bottomRightEurope: QtPositioning.coordinate(51.0, 14.0) 
    property variant viewOfEurope: 
     QtPositioning.rectangle(topLeftEurope, bottomRightEurope) 

    Map { 
     id: mapOfEurope 
     anchors.centerIn: parent; 
     anchors.fill: parent 
     plugin: Plugin { 
      name: "osm" 
     } 
     MapItemView { 
      model: navaidsModel 
      delegate: MapCircle{ 
       center: position 
       radius: 10000 
       color: 'green' 
       border.width: 3 
       MouseArea { 
        anchors.fill: parent 
        onDoubleClicked: { 
         console.log("Doubleclick on " + oaci) 
        } 
        onClicked: { 
         console.log("Point : " + oaci + " " + position + " " + country) 
        } 
       } 
      } 
     } 
     visibleRegion: viewOfEurope 
    } 
} 

은 완전한 예입니다.

+0

Eyllanesc, 당신은 놀라운 일을했고 많은 도움을 받았습니다. 이제이 전체 과정을 자세히 이해하는 데 조금 시간이 걸릴 것입니다. 정말 고마워. – kontiki

+0

수락 완료. 나는 코드, 특히 navaids 모델을 계속 연구합니다. 그것의 부분과 이해하기 어려운 메커니즘 사이에 매우 복잡한 링크가있는 것처럼 보이지만, 나는 포기하지 않을 것입니다. Thanks again again eyllanesc – kontiki

+0

다음을 읽어보십시오. https://doc-snapshots.qt.io/qt5-dev/qtquick-modelviewsdata-modelview.html – eyllanesc

관련 문제