关联文章:
C++ 比较好用的地图库 (尝试) QGeoView-CSDN博客
原程序:
cpp
/***************************************************************************
* 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;
};
cpp
/***************************************************************************
* 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()
{
}
cpp
/***************************************************************************
* 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();
}
存在问题:
cpp
auto osmLayer = new QGVLayerOSM();
mMap->addItem(osmLayer);
basic 示例 = 创建一个窗口 → 放一个地图控件 → 加一个 OSM 图层 → 缩放到全球视图
这个模式好像是损坏的,加载不出来,无法正常显示地图。
要不试试使用Google_HVBRID?
修改:
cpp
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 的矢量街道地图(有道路、地名标注,没有卫星影像)
cpp
// Background layer - Google Hybrid (satellite + labels)
auto googleLayer = new QGVLayerGoogle(QGV::TilesType::Schema);
mMap->addItem(googleLayer);


再试试BING_SATELLITE
显示 Bing Maps 的纯卫星影像(无文字标注)
cpp
// Background layer - Bing Satellite
auto bingLayer = new QGVLayerBing(QGV::TilesType::Satellite);
mMap->addItem(bingLayer);


代码分析:
cpp
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));
}
这四步分别做了什么
| 步骤 | 代码 | 实际效果 |
|---|---|---|
| 1 | new QGVMap() |
创建一个基于 QGraphicsView 的地图控件,内部初始化了投影(EPSG:3857)、相机、根节点 |
| 2 | setupCachedNetworkAccessManager() |
给 QNetworkAccessManager 设置磁盘缓存目录,瓦片图片下载后会存到本地 |
| 3 | new QGVLayerOSM() + addItem() |
创建一个 OSM 在线瓦片图层,加入地图。图层会根据当前视野自动计算需要哪些 (z,x,y) 瓦片并下载 |
| 4 | cameraTo(scaleTo(target)) |
把相机调整到能看到整个世界的缩放级别(z=0,全球一张瓦片) |
运行后你看到什么
bash
┌──────────────────────────────────────────────┐
│ MainWindow (QMainWindow) │
│ ┌──────────────────────────────────────── │
│ │ │ │
│ │ QGVMap (QWidget) │ │
│ │ ┌──────────────────────────────────┐ │ │
│ │ │ QGraphicsView │ │ │
│ │ │ ┌────────────────────────────┐ │ │ │
│ │ │ │ QGraphicsScene │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ │ ┌─ QGVLayerOSM ──────┐ │ │ │ │
│ │ │ │ │ ┌──┐┌──┐┌── │ │ │ │ │
│ │ │ │ │ │瓦││片││ │ ... │ │ │ │ │
│ │ │ │ │ └──┘└──┘└──┘ │ │ │ │ │
│ │ │ │ └────────────────────┘ │ │ │ │
│ │ │ └────────────────────────────┘ │ │ │
│ │ └────────────────────────────────┘ │ │
│ └────────────────────────────────────────┘ │
└──────────────────────────────────────────────┘
一个窗口,里面显示一张世界地图(OSM 的 z=0级别,就是一张 256×256 的瓦片拉伸到全屏)。