qgeoview-samples-basic 关联文章C 比较好用的地图库 (尝试) QGeoView-CSDN博客原程序/*************************************************************************** * QGeoView is a Qt / C widget for visualizing geographic data. * Copyright (C) 2018-2025 Andrey Yaroshenko. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see https://www.gnu.org/licenses. ****************************************************************************/ #pragma once #include QMainWindow #include QGeoView/QGVMap.h class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(); ~MainWindow(); private: QGVMap* mMap; };/*************************************************************************** * QGeoView is a Qt / C widget for visualizing geographic data. * Copyright (C) 2018-2025 Andrey Yaroshenko. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see https://www.gnu.org/licenses. ****************************************************************************/ #include mainwindow.h #include QTimer #include QGeoView/QGVLayerOSM.h #include helpers.h MainWindow::MainWindow() { setWindowTitle(QGeoView Samples - basic); mMap new QGVMap(this); setCentralWidget(mMap); Helpers::setupCachedNetworkAccessManager(this); // Background layer auto osmLayer new QGVLayerOSM(); mMap-addItem(osmLayer); // Show whole world QTimer::singleShot(100, this, [this]() { auto target mMap-getProjection()-boundaryGeoRect(); mMap-cameraTo(QGVCameraActions(mMap).scaleTo(target)); }); } MainWindow::~MainWindow() { }/*************************************************************************** * QGeoView is a Qt / C widget for visualizing geographic data. * Copyright (C) 2018-2025 Andrey Yaroshenko. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see https://www.gnu.org/licenses. ****************************************************************************/ #include QApplication #include QCommandLineParser #include mainwindow.h int main(int argc, char* argv[]) { QApplication app(argc, argv); app.setApplicationName(QGeoView Samples); QCommandLineParser parser; parser.addHelpOption(); parser.addVersionOption(); parser.process(app); MainWindow window; window.show(); return app.exec(); }存在问题auto osmLayer new QGVLayerOSM(); mMap-addItem(osmLayer);basic 示例 创建一个窗口 → 放一个地图控件 → 加一个 OSM 图层 → 缩放到全球视图这个模式好像是损坏的加载不出来无法正常显示地图。要不试试使用Google_HVBRID修改MainWindow::MainWindow() { setWindowTitle(QGeoView Samples - basic); mMap new QGVMap(this); setCentralWidget(mMap); Helpers::setupCachedNetworkAccessManager(this); // Background layer auto osmLayer new QGVLayerOSM(); mMap-addItem(osmLayer); // Show whole world QTimer::singleShot(100, this, [this]() { auto target mMap-getProjection()-boundaryGeoRect(); mMap-cameraTo(QGVCameraActions(mMap).scaleTo(target)); }); }这版程序可以我想再试试Google_SCHEMA模式街道图模式Google 的矢量街道地图有道路、地名标注没有卫星影像// Background layer - Google Hybrid (satellite labels) auto googleLayer new QGVLayerGoogle(QGV::TilesType::Schema); mMap-addItem(googleLayer);再试试BING_SATELLITE显示 Bing Maps 的纯卫星影像无文字标注// Background layer - Bing Satellite auto bingLayer new QGVLayerBing(QGV::TilesType::Satellite); mMap-addItem(bingLayer);代码分析MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { // ① 创建地图控件 mMap new QGVMap(this); setCentralWidget(mMap); // ② 启用网络缓存瓦片会缓存到磁盘避免重复下载 Helpers::setupCachedNetworkAccessManager(this); // ③ 添加 OSM 背景图层 auto osmLayer new QGVLayerOSM(); mMap-addItem(osmLayer); // ④ 相机缩放到全球范围 auto target mMap-getProjection()-boundaryGeoRect(); mMap-cameraTo(QGVCameraActions(mMap).scaleTo(target)); }这四步分别做了什么步骤代码实际效果1new QGVMap()创建一个基于QGraphicsView的地图控件内部初始化了投影EPSG:3857、相机、根节点2setupCachedNetworkAccessManager()给QNetworkAccessManager设置磁盘缓存目录瓦片图片下载后会存到本地3new QGVLayerOSM()addItem()创建一个OSM 在线瓦片图层加入地图。图层会根据当前视野自动计算需要哪些 (z,x,y) 瓦片并下载4cameraTo(scaleTo(target))把相机调整到能看到整个世界的缩放级别z0全球一张瓦片运行后你看到什么┌──────────────────────────────────────────────┐ │ MainWindow (QMainWindow) │ │ ┌──────────────────────────────────────── │ │ │ │ │ │ │ QGVMap (QWidget) │ │ │ │ ┌──────────────────────────────────┐ │ │ │ │ │ QGraphicsView │ │ │ │ │ │ ┌────────────────────────────┐ │ │ │ │ │ │ │ QGraphicsScene │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ┌─ QGVLayerOSM ──────┐ │ │ │ │ │ │ │ │ │ ┌──┐┌──┐┌── │ │ │ │ │ │ │ │ │ │ │瓦││片││ │ ... │ │ │ │ │ │ │ │ │ │ └──┘└──┘└──┘ │ │ │ │ │ │ │ │ │ └────────────────────┘ │ │ │ │ │ │ │ └────────────────────────────┘ │ │ │ │ │ └────────────────────────────────┘ │ │ │ └────────────────────────────────────────┘ │ └──────────────────────────────────────────────┘一个窗口里面显示一张世界地图OSM 的 z0级别就是一张 256×256 的瓦片拉伸到全屏。