我正在使用 OSM 和 QML 应用程序的地图插件。我map.activeMapType = map.supportedMapTypes[currentIndex]在 ComboBox 中使用来在地图区域上显示地图提供程序支持的地图类型。这里地图插件使用"here.app_id"和"here.token"参数。但对于 OSM 插件,地形、交通和除街道地图图块之外的其他图块显示“需要 API 密钥”。我从Thunderforest.com获得了 API 密钥。使用该参数时,仍然显示“需要 API Key”:
ComboBox {
id: selectmap
width: parent.width
model:map.supportedMapTypes
textRole:"description"
onCurrentIndexChanged:{
map.activeMapType = map.supportedMapTypes[currentIndex]
}
}
Plugin {
id: pluginOSM
name: "osm"
PluginParameter {
name: "osm.mapping.providersrepository.address";
// name: "osm.geocoding.host"; (also didn't work)
value: "https://tile.thunderforest.com/transport/{z}/{x}/{y}.png?apikey=<my_api_key>" }
}
Run Code Online (Sandbox Code Playgroud)
我还从http://maps-redirect.qt.io/osm/5.8/站点下载了地形文件参数,以便与 qrc 一起使用,如下所示:
import QtQuick 2.6
import QtQuick.Controls 2.0
import QtLocation 5.12
import QtPositioning 5.12
ApplicationWindow{
id: root
width: 500
height: 700
visible: true
Flickable {
height: parent.height
width: parent.width
clip: true
boundsBehavior: Flickable.StopAtBounds
contentHeight: Math.max(mapColumn.implicitHeight, height)+50
ScrollBar.vertical: ScrollBar {}
z: 2
Column{
anchors.horizontalCenter: parent.horizontalCenter
id:mapColumn
spacing: 5
anchors.fill : parent
Row{
anchors.horizontalCenter: parent.horizontalCenter
spacing:25
id:maprow
Rectangle{
width:mapColumn.width
height:mapColumn.height/2
Map {
id:map
anchors.fill: parent
plugin: Plugin {
name: "osm"
PluginParameter {
name: "osm.mapping.host";
value: "qrc:/terrain"
}
}
}
}
}
Column{
id: combos
spacing: 10
width: parent.width
anchors.verticalCenter: root.verticalCenter
Row{
anchors.horizontalCenter: parent.horizontalCenter
spacing:1
Label{ text:"Map Type: " }
// Map Types
ComboBox {
id: selectmap
width: 200
model:map.supportedMapTypes
textRole:"description"
onCurrentIndexChanged: map.activeMapType = map.supportedMapTypes[currentIndex]
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在地形文件中,我更新了参数,因为"UrlTemplate" : "https://tile.thunderforest.com/landscape/{z}/{x}/{y}.png?apikey=<api-key>",
这不起作用,自定义地图视图为空。是否可以使用 API 密钥将其删除?谢谢
小智 8
(从我的博客文章复制:http://blog.mikeasoft.com/2020/06/22/qt-qml-maps-using-the-osm-plugin-with-api-keys/)
\n它\xe2\x80\x99并不明显,但是在深入研究了OSM插件的工作方式之后,我\xe2\x80\x99发现了一种机制,通过该机制可以将API密钥提供给需要API密钥的磁贴服务器。
\nWhen the OSM plugin is initialised it communicates with the Qt providers repository which tells it what URLs to use for each map type. The location of the providers repository can be customised through the osm.mapping.providersrepository.address OSM plugin property, so all we need to do to use our API key is to set up our own providers repository with URLs that include our API key as a parameter. The repository itself is just a collection of JSON files, with specific names (cycle, cycle-hires, hiking, hiking-hires, night-transit, night-transit-hires, satellite, street, street-hires, terrain, terrain-hires, transit, transit-hires) each corresponding to a map type. The *-hires files provide URLs for tiles at twice the normal resolution, for high DPI displays.
\nFor example, this is the cycle file served by the default Qt providers repository:
\n{\n "UrlTemplate" : "http://a.tile.thunderforest.com/cycle/%z/%x/%y.png",\n "ImageFormat" : "png",\n "QImageFormat" : "Indexed8",\n "ID" : "thf-cycle",\n "MaximumZoomLevel" : 20,\n "MapCopyRight" : "<a href=\'http://www.thunderforest.com/\'>Thunderforest</a>",\n "DataCopyRight" : "<a href=\'http://www.openstreetmap.org/copyright\'>OpenStreetMap</a> contributors"\n}\nRun Code Online (Sandbox Code Playgroud)\nTo provide an API key with our tile requests we can simply modify the UrlTemplate:
\n "UrlTemplate" : "http://a.tile.thunderforest.com/cycle/%z/%x/%y.png?apikey=YOUR_API_KEY",\nRun Code Online (Sandbox Code Playgroud)\nI\xe2\x80\x99ve created a simple tool for setting up a complete repository using a custom API key here: https://github.com/Elleo/qt-osm-map-providers
\ngit clone https://github.com/Elleo/qt-osm-map-providers.git./set_api_keys.sh your_api_key (replacing your_api_key with the key you obtained in step 1)Here\xe2\x80\x99s a quick example QML app that will make use of the custom repository we\xe2\x80\x99ve set up:
\nimport QtQuick 2.7\nimport QtQuick.Controls 2.5\nimport QtLocation 5.10\n\nApplicationWindow {\n\n title: qsTr("Map Example")\n width: 1280\n height: 720\n\n Map {\n anchors.fill: parent\n zoomLevel: 14\n plugin: Plugin {\n name: "osm"\n PluginParameter { name: "osm.mapping.providersrepository.address"; value: "http://www.mywebsite.com/osm_repository" }\n PluginParameter { name: "osm.mapping.highdpi_tiles"; value: true }\n }\n activeMapType: supportedMapTypes[1] // Cycle map provided by Thunderforest\n }\n \n}\nRun Code Online (Sandbox Code Playgroud)\n\n