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

相关推荐
Omics Pro1 小时前
微软:多模态生物世界模型
数据库·人工智能·算法·microsoft·机器学习·自然语言处理
字节渡客2 小时前
接口明明返回成功,数据库偶尔就是没写入
数据库
Ivanqhz2 小时前
KV Cache
服务器·数据库·人工智能·深度学习·算法
Omics Pro2 小时前
~30,000+引用!理论2005,R包2008,多组学集成AI增强
开发语言·数据库·人工智能·算法·机器学习·自然语言处理·r语言
呆萌很3 小时前
数据库逻辑结构设计
数据库
howdoyoudo2026063 小时前
当新案例冲击旧框架:分类系统的宿命与修正路径
大数据·网络·数据库·人工智能·安全·ai·分类
自己的九又四分之三站台3 小时前
GeoPackage 到底适合什么场景:从 SQLite 容器到空间数据格式选型
数据库·oracle·sqlite
东方护航数据恢复(深圳)3 小时前
勒索病毒加密数据库怎么办?深圳 0 赎金解密恢复实录【东方护航数据恢复深圳店】
数据库·数据恢复·服务器数据恢复·勒索病毒·数据镜像
倔强的石头_3 小时前
应用账号最小权限实践:读写账号、报表账号、运维账号分层
数据库