qgeoview-samples-basic

关联文章:

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 的瓦片拉伸到全屏)。

相关推荐
独隅27 分钟前
Claude API 实战教程:从入门到完整项目
android·java·数据库·claude·claude code
_孤傲_33 分钟前
快速掌握 MySQL 语法
数据库·后端·mysql
数字新视界2 小时前
一体化机柜在数据中心运维中的创新应用与优势分析
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
SelectDB3 小时前
从 StarRocks 走向 Apache Doris:如何实现引擎平滑替换与架构升级?
大数据·数据库·数据分析
为思念酝酿的痛3 小时前
MySQL的操作
数据库·mysql
猫咪很可爱3 小时前
Oracle ORA-4031 错误分析与处理:SGA 内存不足导致 update/exp/expdp 失败
数据库·oracle
小豆庄4 小时前
视频素材处理时的隐私盲区:云处理与本地处理的边界在哪
数据库·图像处理·视频·敏捷流程
Umi·4 小时前
MySQL库表操作
数据库·mysql
风哥2号5 小时前
数据库教程FGMT69:PostgreSQL运维诊断与监控分析
运维·数据库·postgresql
ss2735 小时前
AI时代首先数据库-PostgreSQL实战笔记 27 PostgreSQL数据写入与读取流程解析
数据库·笔记·postgresql