(一)QML加载离线地图+标记坐标点

1、实现效果

  • 加载离线地图瓦片、鼠标拖拽、滚轮缩放
  • 在地图上固定坐标位置标注地名

(一)QML加载离线地图+标记坐标点:MiniMap-mini

2、实现方法

2.1、使用工具下载离线地图

不废话,直接搬别人的砖,曰:他山之石可以攻玉!

2.2、QML加载离线地图

官网上有参考例子:QtLocation: using offline map tiles with the OpenStreetMap plugin,完全不需要像很多网站上说的那样骚操作。网页上说明了QtLocation加载离线地图的原理:指定了离线地图路径后,plugin会优先从该路径中查瓦片,找不到才从provider获取该瓦片。(嗯,好像无懈可击!)

With QtLocation 5.8 it is possible to pass an additional offline directory to theOSM plugin. When this parameter is present, tiles will be sourced from the specified directory before being searched in the tile cache, and, after that, being requested to the provider.

关键点在于将离线地图放到qrc资源文件中,这样就可以用相对路径来访问离线地图瓦片了。如果不这样做,osm.mapping.offline.directory就只能采用绝对路径。

复制代码
    Plugin {
        id: mapPlugin
        name: "osm"

        PluginParameter{
            name: "osm.mapping.offline.directory"
            value: ":/offline_tiles/"
        }
    }

经验之谈:当zoomLevel较大时,qrc资源文件放入的瓦片太多了,编译会很慢,甚至Qt会卡死。因此,需要选择恰当的zoomLevel。

2.3、在地图上做标记

标记其实很简单,在指定坐标上添加图像即可,使用MapQuickItem来实现。

复制代码
        // 成都
        MapQuickItem {
            id: chengdu
            anchorPoint.x: image_axis_x()
            anchorPoint.y: image_axis_y()
            coordinate: QtPositioning.coordinate(30.659462, 104.065735)
            sourceItem: Column {Image{id: img1; source: "marker.png"}}
        }

让人不爽的是,地图在缩放过程中,Marker标记位置并不精准,还需要优化。

2.4、GitCode源码分享

QT / MiniMap · GitCode离线地图上标注坐标点https://gitcode.net/qt4/MiniMap.git

相关推荐
Quz2 天前
QML 文字入场动画:逐字弹出、逐字飞入、3D 飞入
qt·3d·动画·qml
Quz4 天前
QML 基础组件专栏目录(V1)
qt·qml
不想学习!!18 天前
Qt Quick 常用控件入门:Window、Button、CheckBox 与 RadioButton
qt·qml
人还是要有梦想的1 个月前
QT qml布局讲解
qt·布局·qml
Rookie Linux1 个月前
使用Qt6 QML以及第三方库FluentUI、PCapPlusPlus开发一个自定义抓包软件
网络·c++·qt·cmake·qml
谁刺我心1 个月前
[QtCPP]Examples使用示例-QtMultimedia、QMediaPlayer、Audio音频引擎测试mp3播放
qt·音视频·qml
Quz1 个月前
Qt Quick 粒子系统(二):系统控制与生命周期管理
qt·qml·粒子系统
Quz1 个月前
Qt Quick 粒子系统(三):发射器深度解析
qt·qml·粒子系统
nnnnichijou2 个月前
Qt 6.9 嵌入式 Linux 交叉编译全栈填坑指南(以树莓派5 AArch64 为例
qt·嵌入式·交叉编译·qml·树莓派5
Quz2 个月前
Qt Quick 粒子系统(一):架构总览与四层模型
qt·架构·qml