首页 > 解决方案 > 在 qt 地图中使用 tileserver-gl 主机

问题描述

我已经设置了一个本地切片服务器以与我的应用程序一起使用,但是当我创建 QML Map 对象并指定插件以使用自定义主机时,应用程序不使用本地切片。在地图对象上循环supportedMapTypes和设置activeMapType属性将显示一些图块,但它们似乎是一些默认图块集,而不是来自我本地图块服务器的图块。

Map
{
    id: map
    objectName: "basemap"
    anchors.fill: parent
    plugin: Plugin
    {
        name: "osm"
        PluginParameter
        {
            name: "osm.mapping.custom.host"
            value: "http://localhost:8080/data/openmaptiles_satellite_lowres/"
        }
    }
    zoomLevel: 1
    activeMapType: // varies depending on selection from another object in application
    center: QtPositioning.coordinate(0, 0)
}

我知道磁贴服务器运行正常,因为我可以通过导航在浏览器中访问它,localhost:8080并且可以使用访问任意磁贴http://localhost:8080/data/openmaptiles_satellite_lowres/{z}/{y}/{x}.jpg

更新

我正在尝试覆盖 TomasL 下面建议的默认提供程序存储库文件,但应用程序似乎没有使用指定的插件参数。

Mapper.qml 中的地图组件

Map {
  id: basemap
  objectName: "basemap"
  anchors.fill: parent
  plugin: ProvidersPlugin {}

  activeMapType: supportedMapTypes[1] // To use the satellite file in providers repository
  center: QtPositioning.coordinate(0, 0)
  zoomLevel: 2
  minimumZoomLevel: 0
  maximumZoomLevel: 5
}

ProvidersPlugin.qml

import QtLocation 5.5
import QtPositioning 5.5

Plugin {
  id: mapPlugin

  name: "osm"

  PluginParameter {
    name: "osm.mapping.providersrepository.address"
    value: Qt.resolvedUrl('./providers')
  }
}

./供应商/卫星

{
  "Enabled" : true,
  "UrlTemplate" : "http://localhost:8080/data/openmaptiles_satellite_lowres/%z/%x/%y.jpg",
  "ImageFormat" : "jpg",
  "QImageFormat" : "Indexed8",
  "MapCopyRight" : "Test",
  "DataCopyRight" : "Hello World",
  "MinimumZoomLevel" : 0,
  "MaximumZoomLevel" : 5,
}

使用上面的代码,我的应用程序仍然尝试访问默认服务器otile1.mqcdn.com

标签: qtqmlmapstileserver-gl

解决方案


问题是您使用的媒体提供 jpg 图像,但 Qt OSM 插件仅支持 png 格式。一种解决方案是克隆 Qt Location 模块,修改源代码以便可以设置、编译和安装图像格式。

为了简化该任务,我为 Qt 5.15.1 创建了一个补丁:

tile_image_format.patch

diff --git a/src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp b/src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp
index 22c32342..d4747a0a 100644
--- a/src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp
+++ b/src/plugins/geoservices/osm/qgeotiledmappingmanagerengineosm.cpp
@@ -217,11 +217,16 @@ QGeoTiledMappingManagerEngineOsm::QGeoTiledMappingManagerEngineOsm(const QVarian
         if (parameters.contains(QStringLiteral("osm.mapping.copyright")))
             m_customCopyright = parameters.value(QStringLiteral("osm.mapping.copyright")).toString();
 
+        QString format = "png";
+        if(parameters.contains(QStringLiteral("osm.mapping.custom.format"))){
+            format = parameters.value(QStringLiteral("osm.mapping.custom.format")).toString();
+        }
+
         m_providers.push_back(
             new QGeoTileProviderOsm( nmCached,
                 QGeoMapType(QGeoMapType::CustomMap, tr("Custom URL Map"), tr("Custom url map view set via urlprefix parameter"), false, false, 8, pluginName, cameraCaps),
-                { new TileProvider(tmsServer + QStringLiteral("%z/%x/%y.png"),
-                    QStringLiteral("png"),
+                { new TileProvider(tmsServer + QStringLiteral("%z/%x/%y.") + format,
+                    format,
                     mapCopyright,
                     dataCopyright) }, cameraCaps
                 ));

上述步骤可概括为:

git clone -b 5.15.1 https://github.com/qt/qtlocation.git
cd qtlocation/src/plugins/geoservices/osm
wget https://raw.githubusercontent.com/eyllanesc/stackoverflow/master/questions/64391146/tile_image_format.patch
git apply tile_image_format.patch
qmake
make
make install

另一方面,您必须在 MapType.CustomMap 中指向 activeMapType:

Map
{
    id: map
    anchors.fill: parent
    plugin: Plugin
    {
        name: "osm"
        PluginParameter
        {
            name: "osm.mapping.custom.host"
            value: "http://localhost:8080/data/openmaptiles_satellite_lowres/"
        }
        PluginParameter
        {
            name: "osm.mapping.custom.format"
            value: "jpg"
        }
    }
    zoomLevel: 1
    center: QtPositioning.coordinate(0, 0)
    activeMapType: MapType.CustomMap
}

在此处输入图像描述


推荐阅读