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

相关推荐
OceanRoy2 小时前
数据库表的设计
数据库·内连接·外连接·er图·表的约束·三大范式·多表连接
Pniubi3 小时前
怎么排查慢SQL,怎么解决?
java·数据库·sql
Decay__7 小时前
VMware 安装 SQL Server 双节点 AlwaysOn 可用性组教程 —— WSFC、AG 与侦听器完整部署(系列核心篇)
运维·数据库·sql·mysql·架构
茶栀(*´I`*)8 小时前
SQL 性能分析:从慢查询日志到执行计划的完整定位方法
数据库·sql
艺杯羹8 小时前
1Panel 装好后 ufw 仍是 inactive:一次服务器安全盲区复盘
服务器·数据库·安全·网络安全·系统架构·实战·1panel
code2cat9 小时前
【随笔】MCP工具重试:任务已经创建,响应丢了怎么办
数据库·mcp
wuyk55511 小时前
Spider-第10讲-SQLite持久化与去重
数据库·爬虫·sqlite
乌暮13 小时前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring
小此方13 小时前
Re:Mysql数据库基础篇(十二):什么是事务、事务解决了什么问题、事务的四大属性、事务的操作方式
数据库·mysql
flash俊杰14 小时前
v0.5-01 让桌面 AI 助手记得住:预算组装、递归摘要、长期记忆与评估基线
数据库·安全·架构