问题描述
我已经设置了一个本地切片服务器来与我的应用程序一起使用,但是当我创建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')
}
}
./ providers / satellite
{
"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
解决方法
问题是所使用的媒体提供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,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
另一方面,您必须将ActiveMapType指向MapType.CustomMap:
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
}
,
一种无需Qt修补程序的简单方法是按原样使用OpenStreetMap插件,但为该插件设置另一个提供程序存储库地址。
<script type="text/javascript">
$(document).ready(function() {
const deg = 6;
const hr = document.querySelector('#hr');
const mn = document.querySelector('#mn');
const sc = document.querySelector('#sc');
let day = new Date();
let hh = day.getHours() * 30;
let mm = day.getMinutes * deg;
let ss = day.getSeconds * deg;
hr.style.transform = `rotateZ(${(hh)+(mm/12)}deg)`;
mn.style.transform = `rotateZ(${mm}deg)`;
sc.style.transform = `rotateZ(${ss}deg)`;
});
</script>
然后,您可以将所有名为q,远足,夜间运输,卫星,街道,地形,运输的地图图层重定向文件放在qrc:/ Common / Engine / Source / Plugins / N / NRedirect文件夹中。如果您愿意仅使用卫星图层,然后跳过所有其他文件。卫星文件可能看起来像这样(这才是真正的魔力所在!)
import QtLocation 5.12
Plugin {
id: pluginN
property string projectname: ""
name: "osm"
PluginParameter {
name: "osm.mapping.providersrepository.address"
value: "qrc:/Common/Engine/Source/Plugins/N/NRedirect/" //or wherever you place your redirect files
}
PluginParameter { name: "osm.mapping.copyright"; value: "(c) N /OpenStreetMap" }
}
请注意,在您的示例网址中,http:// localhost:8080 / data / openmaptiles_satellite_lowres / {z} / {y} / {x} .jpg ,z / x / y的顺序与大多数示例不同。如果以此方式设置服务器,则需要更改上面的UrlTemplate。
有关此操作的更多详细信息,请参见osm插件的源代码: https://github.com/qt/qtlocation/blob/dev/src/plugins/geoservices/osm/qgeotileproviderosm.cpp#L392-L568