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

QT5.14版本加载在线地图(天地图)

  1. 在pro文件中添加:QT += webenginewidgets
    如图

  2. 添加头文件:也可以自定义一个QWebEngineView

  3. #include <QWebEngineHistory> #include <QWebEngineHistoryItem> #include <QWebEnginePage> #include <QWebEngineView> #include <QtWebEngineWidgets/QtWebEngineWidgets>

  4. 自定义的头文件和源文件

#include "cmywebengineview.h"CMyWebEngineView::CMyWebEngineView(QWidget *parent) : QWebEngineView(parent)
{m_pContextMenu = new QMenu;m_pBackAct = new QAction(QIcon::fromTheme(QStringLiteral("go-previous")), QStringLiteral("返回"));m_pForwardAct = new QAction(QIcon::fromTheme(QStringLiteral("go-next")), QStringLiteral("前进"));m_pReloadAct = new QAction(QIcon::fromTheme(QStringLiteral("view-refresh")), QStringLiteral("刷新"));m_pContextMenu->addAction(m_pBackAct);m_pContextMenu->addAction(m_pForwardAct);m_pContextMenu->addAction(m_pReloadAct);connect(m_pBackAct, &QAction::triggered, this, &QWebEngineView::back);connect(m_pForwardAct, &QAction::triggered, this, &QWebEngineView::forward);connect(m_pReloadAct, &QAction::triggered, this, &QWebEngineView::reload);setContextMenuPolicy(Qt::DefaultContextMenu);
}CMyWebEngineView::~CMyWebEngineView()
{
}void CMyWebEngineView::contextMenuEvent(QContextMenuEvent* event)
{m_pBackAct->setEnabled(this->history()->canGoBack());m_pForwardAct->setEnabled(this->history()->canGoForward());m_pContextMenu->exec(QCursor::pos());
}```cpp
头文件:
#ifndef CMYWEBENGINEVIEW_H
#define CMYWEBENGINEVIEW_H#include <QWebEngineView>
#include <QMenu>
#include <QAction>
#include <QIcon>
#include <QWebEngineHistory>class CMyWebEngineView : public QWebEngineView
{Q_OBJECT
public:CMyWebEngineView(QWidget* parent = nullptr);~CMyWebEngineView();
protected:void contextMenuEvent(QContextMenuEvent* event);
private:QMenu*      m_pContextMenu;QAction*    m_pBackAct;QAction*    m_pForwardAct;QAction*    m_pReloadAct;
};#endif // CMYWEBENGINEVIEW_H
  • 在ui界面拖一个Qwidget然后提升空间为上面的QEngineView如图:提升后类名称为自定义的类

  • 到地图开放平台上申请一个key值用于以下html:

<!DOCTYPE html>
<html>
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="keywords" content="天地图"/><title>天地图-地图API-范例-经纬度直投地图</title><script type="text/javascript" src="http://api.tianditu.gov.cn/api?v=4.0&tk=你的Key值"></script><script type="text/javascript" src="qwebchannel.js"></script>   <!-- 与qt交互 --><style type="text/css">body,html{width:100%;height:100%;margin:0;font-family:"Microsoft YaHei"}#mapDiv{width:100%;height:640px}input,b,p{margin-left:5px;font-size:14px}</style><script>var map;var zoom = 12;function onLoad() {map = new T.Map('mapDiv', {projection: 'EPSG:4326'});map.centerAndZoom(new T.LngLat(116.40769, 39.89945), zoom);//允许鼠标双击放大地图map.enableScrollWheelZoom();//创建标注工具对象markerTool = new T.MarkTool(map, {follow: true});}function getMapCenter() {alert("当前地图中心点:" + map.getCenter().getLng() + "," + map.getCenter().getLat());
}function editMarker() {var markers = markerTool.getMarkers()for (var i = 0; i < markers.length; i++) {markers[i].enableDragging();}}function endeditMarker() {var markers = markerTool.getMarkers()for (var i = 0; i < markers.length; i++) {markers[i].disableDragging();}}// qt交互注册new QWebChannel(qt.webChannelTransport,function(channel){window.JSInterface = channel.objects.JSInterface; // 注册});</script>
</head>
<body onLoad="onLoad()">
<div id="mapDiv"></div>
</body>
</html>

  • 要与qt交互也要确保有js文件,可到qt源代码例程中找到复制过来:(如果没下载的话建议在MaintenanceTool.exe中添加源文件和QWebEngine,没下就不支持)
    一般路径是这个
  • 在初始化函数中加载网页:
    QString CoreStr = QCoreApplication::applicationDirPath();QString htmlPath = CoreStr + "/Js_Map/tiandituMapView.html";QUrl testUrl(CoreStr + "/Js_Map/");QFile file(htmlPath);if (!file.open(QIODevice::ReadOnly)){return;}QString htmlData = file.readAll().constData();file.close();m_WebChannel = new QWebChannel(ui->widget_WebView->page());//创建与一个网页交互的通道ui->widget_WebView->page()->setWebChannel(m_WebChannel);m_WebChannel->registerObject(QString("JSInterface"), ui->widget_WebView);//注册通道,ID为JSInterface,将在js文件中引用ui->widget_WebView->page()->load(QUrl(htmlPath));//加载网页,必须在通道注册后

最后即可显示:
显示如图
目前刚接触到地图这块,后续具体功能未知,一般来说可在开放的api中设置,挺强大的
天地图API例子

http://www.xdnf.cn/news/596575.html

相关文章:

  • 【C语言】复习~动态内存管理
  • LRU (Least Recently Used) 页面置换算法
  • 征程 6 J6E/M linear 双int16量化支持替代方案
  • 什么是云主机?
  • 使用行为分析和深度证据集群实时检测内部威胁
  • deepwiki-open开源项目分析
  • CVE-2022-22947源码分析与漏洞复现
  • 堆的C语言实现
  • 认识CPU (三):数据通路——CPU的煎饼物流系统
  • 汇舟问卷:国外问卷调查如何闭坑
  • 并发编程实战--对象的共享
  • java每日精进 5.22【多数据源(读写分离)、事务】
  • 01_springCloud基础知识
  • 并发编程之线程基础
  • 『VUE』vue-quill-editor 添加超链接的同时为文字添加颜色(详细图文注释)
  • 有动画效果,但动画窗格里为空
  • 红黑树插入的旋转变色
  • Python |GIF 解析与构建(1):初步解析
  • SOC-ESP32S3部分:7-如何学习ESP32S3-IDF开发
  • Katoolin3 项目介绍:在 Ubuntu 上轻松安装 Kali Linux 工具
  • 【题解-洛谷】P9644 [SNCPC2019] Turn It Off
  • 1.2V超低功耗晶振:物联网设备续航提升的秘密武器
  • ThreadLocal底层原理解析
  • 比较结构的连通性
  • MySQL多线程备份工具mysqlpump详解!
  • 骰子游戏(2023睿抗省赛)
  • C++函数封装和绑定
  • 硬件,软件和进程
  • 过氧化物酶的邻近标记技术(APEX):最灵敏的蛋白互作方法
  • Python生成物理引擎的简单知识图谱