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

相关推荐
左直拳2 天前
使用Mobile Atlas Creator 2.0获取离线地图
离线地图
水煎包V:YEDIYYDS8882 天前
QT QML 实现的旋钮按钮,类似收音机音量旋钮,可指示方向和角度
qt·qml·旋钮组件
水煎包V:YEDIYYDS8886 天前
QT 在 QML中 嵌入显示qwidget界面显示的两种方式,已在项目中验证
qt·qml·qt开发·qwidget
水煎包V:YEDIYYDS8887 天前
QT QML 实现的摇杆按钮,类似王者荣耀 左边方向导航键
qt·qml·摇杆按钮·导航键
千疑千寻~13 天前
【QML】C++访问QML控件
c++·qml
Wallace Zhang14 天前
PySide6 + QML - Charts07 - 使用checkbox选择需要显示的曲线
vscode·pyside6·qml
千疑千寻~14 天前
【QML】自定义控件
qml
Wallace Zhang1 个月前
PySide6 + QML - 调试日志01 -告别打印log中文乱码,快速且简单地解决
qt·pyside6·qml
江公望1 个月前
Qt QHostInfo::lookupHost()函数,10分钟讲清楚
开发语言·qt·qml
江公望1 个月前
Qt告警clazy-detaching-temporary浅谈
qt·qml