当前位置: 首页 > news >正文

QGraphicsView实现简易地图4『局部加载-地图漫游』

前文链接:QGraphicsView实现简易地图3『局部加载-地图缩放』
当鼠标拖动地图移动时,需要实时增补和删减瓦片地图,大致思路是计算地图从各方向移动时进出视口的瓦片坐标值,根据变化后的瓦片坐标值来增减地图瓦片,以下将提供实现此需求的核心代码。
1、动态演示效果


2、静态展示图片
在这里插入图片描述

核心代码

void MapView::moveScene()
{QString appPath = QApplication::applicationDirPath();QString dirPath = QString("%1/MapData/GaoDeMap/Map/MapPng/L0%2").arg(appPath).arg(m_curLevel + 1);// 视口宽度和高度int w = viewport()->width();int h = viewport()->height();// 计算呈现的瓦片地图左上角的场景坐标和视口坐标、呈现的瓦片地图右下角的场景坐标和视口坐标QPoint topLeftScenePos(m_topLeftTileCoord.x * PIXMAP_SIZE, m_topLeftTileCoord.y * PIXMAP_SIZE);QPointF topLeftViewPos = mapFromScene(topLeftScenePos);QPoint bottomRightScenePos(m_bottomRightTileCoord.x * PIXMAP_SIZE, m_bottomRightTileCoord.y * PIXMAP_SIZE);QPointF bottomRightViewPos = mapFromScene(bottomRightScenePos);// 1、水平瓦片坐标控制:判断最左侧瓦片是否完全进入视口、最右侧瓦片是否完全离开视口if (topLeftViewPos.x() > 0){int count = qCeil(topLeftViewPos.x() / PIXMAP_SIZE);	// 左侧进入视口瓦片数量int oldLeftTileCoordX = m_topLeftTileCoord.x;			// 保存原左侧瓦片坐标Xm_topLeftTileCoord.x -= count;							// 更新现左侧瓦片坐标X// 增加从左侧进入视口的图片for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row){for (int col = m_topLeftTileCoord.x; col < oldLeftTileCoordX; ++col){QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath).arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));QPixmap pixmap(fileName);QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);m_scene->addItem(item);m_mapItems[row][col] = item;}}}if (bottomRightViewPos.x() > w){int count = qFloor((bottomRightViewPos.x() - w) / PIXMAP_SIZE) + 1;	// 右侧离开视口瓦片数量int oldRightTileCoordX = m_bottomRightTileCoord.x;					// 保存原右侧瓦片坐标Xm_bottomRightTileCoord.x -= count;									// 更新现右侧瓦片坐标X// 删除从右侧离开视口的图片for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row){for (int col = oldRightTileCoordX; col > m_bottomRightTileCoord.x; --col){QGraphicsPixmapItem *item = m_mapItems[row][col];m_scene->removeItem(item);m_mapItems[row].remove(col);delete item;}}}// 2、水平瓦片坐标控制:判断最右侧瓦片是否完全进入视口、最左侧瓦片是否完全离开视口if (bottomRightViewPos.x() + 255 < w){int count = qCeil((w - (bottomRightViewPos.x() + 255)) / PIXMAP_SIZE);	// 右侧进入视口瓦片数量int oldRightTileCoordX = m_bottomRightTileCoord.x;						// 保存原右侧瓦片坐标Xm_bottomRightTileCoord.x += count;										// 保存现右侧瓦片坐标X// 增加从右侧进入视口的图片for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row){for (int col = m_bottomRightTileCoord.x; col > oldRightTileCoordX; --col){QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath).arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));QPixmap pixmap(fileName);QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);m_scene->addItem(item);m_mapItems[row][col] = item;}}}if (topLeftViewPos.x() + 255 < 0){int count = qFloor(fabs(topLeftViewPos.x()) / PIXMAP_SIZE);	// 左侧离开视口瓦片数量int oldLeftTileCoordX = m_topLeftTileCoord.x;				// 保存原左侧瓦片坐标Xm_topLeftTileCoord.x += count;								// 保存现左侧瓦片坐标X// 删除从左侧离开视口的图片for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row){for (int col = oldLeftTileCoordX; col < m_topLeftTileCoord.x; ++col){QGraphicsPixmapItem *item = m_mapItems[row][col];m_scene->removeItem(item);m_mapItems[row].remove(col);delete item;}}}// 3、垂直瓦片坐标控制:判断最上侧瓦片是否完全进入视口,最下侧瓦片是否完全离开视口if (topLeftViewPos.y() > 0){int count = qCeil(topLeftViewPos.y() / PIXMAP_SIZE);	// 上侧进入视口瓦片数量int oldTopTileCoordY = m_topLeftTileCoord.y;			// 保存原上侧瓦片坐标Ym_topLeftTileCoord.y -= count;							// 保存现上侧瓦片坐标Y// 增加从上侧进入视口的图片for (int row = m_topLeftTileCoord.y; row < oldTopTileCoordY; ++row){for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col){QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath).arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));QPixmap pixmap(fileName);QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);m_scene->addItem(item);m_mapItems[row][col] = item;}}}if (bottomRightViewPos.y() > h){int count = qFloor((bottomRightViewPos.y() - h) / PIXMAP_SIZE) + 1;	// 下侧离开视口瓦片数量int oldBottomTileCoordY = m_bottomRightTileCoord.y;					// 保存原下侧瓦片坐标Ym_bottomRightTileCoord.y -= count;									// 保存现下侧瓦片坐标Y// 删除从下侧离开视口的图片for (int row = oldBottomTileCoordY; row > m_bottomRightTileCoord.y; --row){for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col){QGraphicsPixmapItem *item = m_mapItems[row][col];m_scene->removeItem(item);m_mapItems[row].remove(col);delete item;}}}// 4、垂直瓦片坐标控制:判断最下侧瓦片是否完全进入视口,最上侧瓦片是否完全离开视口if (bottomRightViewPos.y() + 255 < h){int count = qCeil((h - (bottomRightViewPos.y() + 255)) / PIXMAP_SIZE);	// 下侧进入视口瓦片数量int oldBottomTileCoordY = m_bottomRightTileCoord.y;						// 保存原下侧瓦片坐标Ym_bottomRightTileCoord.y += count;										// 保存现下侧瓦片坐标Y// 增加从下侧进入视口的图片for (int row = m_bottomRightTileCoord.y; row > oldBottomTileCoordY; --row){for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col){QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath).arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));QPixmap pixmap(fileName);QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);m_scene->addItem(item);m_mapItems[row][col] = item;}}}if (topLeftViewPos.y() + 255 < 0){int count = qFloor(fabs(topLeftViewPos.y()) / PIXMAP_SIZE);	// 上侧离开视口瓦片数量int oldTopTileCoordY = m_topLeftTileCoord.y;				// 保存原上侧瓦片坐标Ym_topLeftTileCoord.y += count;								// 保存现上侧瓦片坐标Y// 删除从上侧离开视口的图片for (int row = oldTopTileCoordY; row < m_topLeftTileCoord.y; ++row){for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col){QGraphicsPixmapItem *item = m_mapItems[row][col];m_scene->removeItem(item);m_mapItems[row].remove(col);delete item;}}}
}
http://www.lryc.cn/news/119057.html

相关文章:

  • ubuntu 安装 nvidia 驱动
  • 探索APP界面布局的艺术与技巧:从入门到精通
  • 回归预测 | MATLAB实现POA-CNN-GRU鹈鹕算法优化卷积门控循环单元多输入单输出回归预测
  • 15.3.2 【Linux】系统的配置文件:/etc/crontab,/etc/cron.d/*
  • 新版PMP考试中,敏捷是怎么考的?
  • uniapp-----封装接口
  • [oeasy]python0081_[趣味拓展]ESC键进化历史_键盘演化过程_ANSI_控制序列_转义序列_CSI
  • 第十六次CCF计算机软件能力认证
  • 关于Postman如何配置随请求携带token
  • 逆向破解学习-登山赛车
  • linux下实现生产者和消费者 pv操作
  • 十六、遥感影像识别
  • 源码角度分析@configuration和@component不同
  • 实现分布式事务:Java与MySQL的XA事务协调
  • 如何通过CRM系统进行成功的客户生命周期管理?
  • Leetcode 977. 有序数组的平方
  • vue3中使用toValue
  • 阿里云国际版CDN使用教程!
  • 【docker】Dockerfile构建镜像常用指令:
  • Java课题笔记~ 会话跟踪
  • HbuildX生成安卓签名证书
  • 在Ruoyi中采用Ajax动态生成Echarts图表实践
  • 资讯速递 | ArkUI-X 预览版已正式开源!
  • HTML <select> 标签
  • Flutter:文件上传与下载(下载后预览)
  • [前端系列第6弹]Ajax简明教程:轻松实现Web页面的异步交互
  • ssh-keygen 做好免密登录后不生效
  • 【Java可执行命令】(十八)可视化监控和管理工具 jconsole:获取 JVM的内存使用情况、线程活动、GC 行为等重要指标的可视化工具 ~
  • leetcode做题笔记66
  • 【docker】设置 docker 国内镜像报错,解决方案