QML map modification plug-in source code. Map sets the loading map type in Plugin

Posted by montana111 on Thu, 27 Jan 2022 11:56:19 +0100

Common Map types include traffic Map, topographic Map, satellite Map, etc. the activeMapType attribute is provided in QML Map (taking OSM Map as an example) to read the currently displayed Map type (Note: this attribute is read-only and cannot be used for assignment). The loading search of Map in QML is based on the file name of tile, such as tile named "osm_100-l-5-2-2-3.png", Where 5 represents the type of Map. If you need to modify the Map type, you can customize the function:

function setMapOffLineType(){
    if(myMap.activeMapType.name != "Night Transit Map"){
        for(var a in myMap.supportedMapTypes){
            if(myMap.supportedMapTypes[a].name === "Night Transit Map"){
                myMap.activeMapType = myMap.supportedMapTypes[a]
            }
        }
    }
}

But this function can only be set in component It can only be used after oncompleted, and the Map is loaded with tile data twice. When the amount of tiles is large, it will affect the program initialization time. Therefore, after modifying the source code of the Map plug-in Plugin, I directly set the Map type in the Map plug-in. This method will not modify the value of activeMapType in the Map. The code is as follows:

Plugin {
    id: mapPlugin
    name: "osm" // "mapboxgl", "esri", ...

    PluginParameter{
        name:"osm.mapping.offline.directory"
        value:Analyze.getAppPath()+"/Map/night"//night
    }

    PluginParameter{
        name:"osm.mapping.offline.maptype"
        value:5
    }
}

The steps to modify the map plug-in source code are as follows:

1. Use QtCreator to open the source code of the OSM map plug-in project. Location: F:\Qt.11.2\Src\qtlocation\src\plugins\geoservices\osm

2. Add the variable int m in "qgeofiletilecacheosm.h"_ Offlinemaptype, add the parameter mapType in the constructor and set m_offlineMapType initialization, code:

qgeofiletilecacheosm.h:

    /*Add map type ID*/
    int m_offlineMapType;

    QGeoFileTileCacheOsm(const QVector<QGeoTileProviderOsm *> &providers,
                         const QString &offlineDirectory = QString(),
                         const QString &directory = QString(),
                         const int &mapType = 0,
                         QObject *parent = 0);

/*This is the original constructor, and the map type identifier mapType is added */
//    QGeoFileTileCacheOsm(const QVector<QGeoTileProviderOsm *> &providers,
//                         const QString &offlineDirectory = QString(),
//                         const QString &directory = QString(),
//                         QObject *parent = 0);

qgeofiletilecacheosm.cpp

//Right m_offlineMapType initialization
QGeoFileTileCacheOsm::QGeoFileTileCacheOsm(const QVector<QGeoTileProviderOsm *> &providers,
                                           const QString &offlineDirectory,
                                           const QString &directory,
                                           const int &mapType,
                                           QObject *parent)
:   QGeoFileTileCache(directory, parent), m_offlineDirectory(offlineDirectory), m_offlineData(false),m_offlineMapType(mapType), m_providers(providers)

3. Add custom parameter settings in the "qgeofiledmappingmanagerregeneosm. CPP" file to search for existing parameter attributes. For example, "osm.mapping.offline.directory" is convenient to locate the location in the file, and then add custom parameter code:

/*Define map types in plug-ins*/
    int m_offlineMapType=0;
    if(parameters.contains(QStringLiteral("osm.mapping.offline.maptype")))
        m_offlineMapType = parameters.value(QStringLiteral("osm.mapping.offline.maptype")).toInt();
    QGeoFileTileCacheOsm *tileCache = new QGeoFileTileCacheOsm(m_providers, m_offlineDirectory, m_cacheDirectory,m_offlineMapType);

    /*This initializes the constructor for the original map tile*/
//    QGeoFileTileCacheOsm *tileCache = new QGeoFileTileCacheOsm(m_providers, m_offlineDirectory, m_cacheDirectory,m_offlineMapType);

 

4. In "qgeofiletilecacheosm.cpp", jump to the function qstring tilespectofilename (const qgeotilespec & spec, const qstring & format, const qstring & directory) const override;

The function is to generate the tile name of the tile map and modify the code generated by the tile identification:

/*When the map type is set in the plug-in, it is loaded according to the plug-in settings. If it is not set, it is loaded according to the activeMapType in the map*/
    if(m_offlineMapType){
        filename += QString::number(m_offlineMapType);
    }else{
        filename += QString::number(spec.mapId());
    }

/*Original map type filtering method*/
//    filename += QString::number(spec.mapId());

 

5. Recompile the osm project. The compiled plug-ins are located in the plugins\geoservices path under the root directory of Qt installation disk. Copy the generated plug-in files to the plug-in directory in Qt installation directory (F:\Qt.11.2\msvc2015\plugins\geoservices \) and replace the original plug-ins.

6. Pass the user-defined parameter "osm.mapping.offline.maptype" in the plug-in Plugin of QML, where the value of this parameter is the type identification of map tiles (the identification needs to be > 0). If this parameter is not set, the QML map adopts the default map type.

Topics: qml