(一)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

相关推荐
江公望4 天前
装了新的QtCreator17,没有用Qt5.12自带的QtCreator4,导致QtCreator17无法找到Qt5.12帮助文档
qt·qml
奔跑吧 android5 天前
【Qt】【1. 版本特性介绍】
qt·cpp·qml
Wallace Zhang10 天前
PySide6 + QML - 多线程02 - QThread 生命周期与安全退出
vscode·pyside6·qml
江公望11 天前
如何在Qt QML中定义枚举浅谈
开发语言·qt·qml
Hi2024021712 天前
Qt+Qml客户端和Python服务端的网络通信原型
开发语言·python·qt·ui·网络通信·qml
江公望16 天前
Qt qmlplugindump浅谈
开发语言·qt·qml
江公望20 天前
Qt的QT_QPA_EGLFS_INTEGRATION环境变量浅解
linux·qt·qml
江公望24 天前
通过QQmlExtensionPlugin进行Qt QML插件开发
c++·qt·qml
江公望1 个月前
Qt QtConcurrent使用入门浅解
c++·qt·qml
ajassi20001 个月前
开源 C++ QT QML 开发(二)工程结构
linux·qt·qml