离线地图这个功能是近期才完成的,老早以前就很多人问有没有离线地图的功能,之前也大致了解过如何做离线地图,其实最核心的不是代码,而是如何搞到免费的离线地图文件,离线地图下载器网上大部分都是收费的,而且好像还不便宜,后面找了很多终于找到个简单的可用的,可以指定城市进行离线地图的下载,离线地图一般使用百度的或者高德的居多,其实对于在线地图使用比较熟练的话,那些方法完全一致的,理论上厂家做成的api接口也会统一的,唯一的不同就是引入的js文件不一样,使用方法和处理流程是完全一样的,于是抽空花了点时间重新整理封装了一个百度地图类,同时支持在线地图和离线地图两种模式。
地图类功能特点:
视频控件开源地址:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo
文件名称:videowidget
体验地址:https://gitee.com/feiyangqingyun/QWidgetExe https://github.com/feiyangqingyun/QWidgetExe
文件名称:bin_video_system.zip
#include "frmmaplocal.h"
#include "frmmapweb.h"
#include "ui_frmmaplocal.h"
#include "quiwidget.h"
#include "iconfont.h"
#include "mapbaidu.h"
frmMapLocal::frmMapLocal(QWidget *parent) : QWidget(parent), ui(new Ui::frmMapLocal)
{
ui->setupUi(this);
this->initForm();
this->initTree();
this->initMap();
}
frmMapLocal::~frmMapLocal()
{
delete ui;
}
void frmMapLocal::showEvent(QShowEvent *)
{
static bool isShow = false;
if (!isShow) {
isShow = true;
QTimer::singleShot(100, this, SLOT(loadMap()));
}
}
void frmMapLocal::initForm()
{
connect(AppEvent::Instance(), SIGNAL(saveIpcInfo()), this, SLOT(loadMap()));
ui->widgetRight->setFixedWidth(App::RightWidth);
ui->navTitle1->setText("信息设置");
ui->navTitle2->setText("图层管理");
ui->navTitle1->setLeftIcon(0xf041);
ui->navTitle2->setLeftIcon(0xf1b2);
ui->navTitle2->setRightIcon5(0xf1f8);
}
void frmMapLocal::initTree()
{
ui->treeWidget->clear();
ui->treeWidget->setAnimated(false);
ui->treeWidget->setHeaderHidden(true);
//ui->treeWidget->setIndentation(0);
QStringList texts;
texts << "A" << "B" << "C" << "D";
for (int i = 0; i < texts.count(); i++) {
//添加父节点
QTreeWidgetItem *itemParent = new QTreeWidgetItem(ui->treeWidget);
itemParent->setText(0, QString("图层%1").arg(texts.at(i)));
//循环添加子节点
for (int j = 0; j < 5; j++) {
QTreeWidgetItem *itemChild = new QTreeWidgetItem(itemParent);
itemChild->setText(0, QString("%1%2").arg((j % 2 == 0) ? "地形" : "山脉").arg(j + 1));
QPixmap iconNormal = IconFont::Instance()->getPixmap(QUIConfig::TextColor, (j % 2 == 0) ? 0xe6f2 : 0xe6ed, 18, 20, 20);
itemChild->setIcon(0, iconNormal);
}
}
ui->treeWidget->expandAll();
}
void frmMapLocal::initMap()
{
#ifdef webkit
QWebSettings *webSetting = QWebSettings::globalSettings();
webSetting->setAttribute(QWebSettings::JavascriptEnabled, true);
webSetting->setAttribute(QWebSettings::PluginsEnabled, true);
webSetting->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
webView = new QWebView;
ui->layout->addWidget(webView);
connect(webView->page(), SIGNAL(loadFinished(bool)), this, SLOT(loadFinished()));
#elif webengine
QWebEngineSettings *webSetting = QWebEngineSettings::globalSettings();
webSetting->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
webSetting->setAttribute(QWebEngineSettings::PluginsEnabled, true);
webSetting->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true);
webView = new QWebEngineView;
ui->layout->addWidget(webView);
QWebChannel *channel = new QWebChannel(this);
channel->registerObject("objName", MapData::Instance());
webView->page()->setWebChannel(channel);
connect(MapData::Instance(), SIGNAL(receiveDataFromJs(QVariant)), this, SLOT(receiveData(QVariant)));
connect(webView->page(), SIGNAL(loadFinished(bool)), this, SLOT(loadFinished()));
#elif webie
webView = new QAxWidget;
ui->layout->addWidget(webView);
webView->setControl("{8856F961-340A-11D0-A96B-00C04FD705A2}");
#endif
}
void frmMapLocal::loadMap()
{
QString fileName = QUIHelper::appPath() + "/config/deviceall.html";
QString url = "file:///" + fileName;
MapBaiDu::Instance()->reset();
MapBaiDu::Instance()->setFileName(fileName);
MapBaiDu::Instance()->setSaveFile(false);
MapBaiDu::Instance()->setMapLocal(true);
MapBaiDu::Instance()->setShowOverlayTool(true);
MapBaiDu::Instance()->setEnableClickPoint(true);
//设置默认的中心点坐标,建议采用中心点坐标的方式
MapBaiDu::Instance()->setMapCenterPoint("121.414,31.1828");
//设置默认中心城市,在线地图可设置
//MapBaiDu::Instance()->setMapCenterCity("上海");
#ifndef webie
MapBaiDu::Instance()->setCallFun(QString("%1.receiveData").arg("objName"));
#endif
QStringList deviceNames, deviceAddrs, devicePoints;
#if 0
for (int i = 1; i <= 100; i++) {
deviceNames << QString("摄像头%1").arg(i);
deviceAddrs << QString("测试地址%1").arg(i);
devicePoints << QString("121.%1,31.%2").arg(qrand() % 1000000).arg(qrand() % 1000000);
}
#elif 0
deviceNames << "摄像头1" << "摄像头2" << "摄像头3" << "摄像头4" << "摄像头5";
deviceAddrs << "安波路533弄1号楼" << "北新路8号" << "康桥镇康桥路1200号(御青路)" << "闵行区诸翟镇纪翟南路" << "浦东新区广兰路1080号(紫薇路口)";
devicePoints << "121.534942,31.307706" << "121.572075,31.188825" << "121.57987,31.155795" << "121.292628,31.215278" << "121.626992,31.211056";
#else
//从数据库加载
deviceNames = DBData::IpcInfo_IpcName;
deviceAddrs = DBData::IpcInfo_IpcAddr;
devicePoints = DBData::IpcInfo_IpcPosition;
#endif
MapBaiDu::Instance()->setShowTrafficControl(true);
MapBaiDu::Instance()->setShowNavigationControl(true);
MapBaiDu::Instance()->setMarkerInfo(deviceNames, deviceAddrs, devicePoints);
QString content = MapBaiDu::Instance()->newMap();
//下面为两种方式加载网页,如果内容为空则加载网页文件否则加载内容
//一般为了保密建议加载内容,这样看不到生成的网页文件
//可能在linux上的webkit内核需要用load的方式加载
if (MapBaiDu::Instance()->getSaveFile()) {
#ifdef webkit
webView->load(QUrl(url));
#elif webengine
webView->load(QUrl(url));
#elif webie
webView->dynamicCall("Navigate(const QString&)", url);
#endif
} else {
QUrl baseUrl(QString("%1/config/").arg(QUIHelper::appPath()));
#ifdef webkit
webView->setHtml(content, baseUrl);
#elif webengine
webView->setHtml(content, baseUrl);
#endif
}
}
void frmMapLocal::receiveData(const QVariant &data)
{
//演示如何从地图上标注点获取到经纬度
if (!data.isNull()) {
QStringList list = data.toString().split(",");
double longitude = list.at(0).toDouble();
double latitude = list.at(1).toDouble();
//取小数点后6位
QString strLongitude = QString::number(longitude, 'f', 6);
QString strLatitude = QString::number(latitude, 'f', 6);
ui->txtLongitude->setText(strLongitude);
ui->txtLatitude->setText(strLatitude);
}
}
void frmMapLocal::loadFinished()
{
#ifdef webkit
webView->page()->mainFrame()->addToJavaScriptWindowObject("objName", this);
#endif
}