系列文章目录
前言
本文章介绍如何选择插件及选择插件里不同的地图类型
一、新建工程 Map02并选择插件
具体可参见上一篇文章
map.qml
import QtQuick 2.0
import QtQuick.Window 2.14
import QtLocation 5.6
import QtPositioning 5.6
Window {
visible: true
width: 800
height: 600
Plugin {
id: mapPlugin
//这里可进行不同插件的选择,如:esri,mapboxgl,here,osm等
name: "esri" // "mapboxgl", "esri", ...
}
Map {
anchors.fill: parent
plugin: mapPlugin
center: QtPositioning.coordinate(59.91, 10.75) // Oslo
zoomLevel: 14
}
}
当然Map也可以这样写
Map {
anchors.fill: parent
plugin: Plugin { name: "esri" }
center: QtPositioning.coordinate(59.91, 10.75) // Oslo
zoomLevel: 14
}
二、切换图源
1.先看Map的文档
在qt creator 下,当编辑的光标放在Map上时,按F1可直接进入官方手册,当然也可以在帮助里自己搜索
这里贴一下在线地址:https://doc.qt.io/qt-5/qml-qtlocation-map.html
我们注意到有 supportedMapTypes 这个list,里面存放了支持的类型,还有activeMapType 可用于设置活动MapType
先遍历一下看看supportedMapTypes 里面有什么
Map {
anchors.fill: parent
plugin: Plugin { name: "esri" }
center: QtPositioning.coordinate(59.91, 10.75) // Oslo
zoomLevel: 14
Component.onCompleted: {
console.log("activeMapType: " + activeMapType.name)
for(let i = 0;i < supportedMapTypes.length;i++)
{
console.log(supportedMapTypes[i].name)
}
}
}
输出:
这里可以看到当前活动类型:World Street Map
list 里有 World Imagery 这个就是卫星图
2.图源选择
Map {
anchors.fill: parent
plugin: Plugin { name: "esri" }
center: QtPositioning.coordinate(59.91, 10.75) // Oslo
minimumZoomLevel: 2 //最小层级
maximumZoomLevel: 20 //最大层级
zoomLevel: 14//地图当前层级
Component.onCompleted: {
for(let i = 0;i < supportedMapTypes.length;i++)
{
console.log(supportedMapTypes[i].name)
if(supportedMapTypes[i].name === "World Imagery")
{
activeMapType = supportedMapTypes[i];//设置到当前类型
break
}
}
console.log("activeMapType: " + activeMapType.name)
}
}
实际上这个是可以动态切换的,你可以在程序运行的任何时间进行切换图源,不过好像这种需求比较小
总结
就先这么一点吧,一下篇继续,下一篇将以卫星图为基础来绘制点,线,面,航线等