## 一、前言说明
之前就已经实现过这个功能,近期用户反馈某些区域下载不正确,仔细检查原来是因为行政区域同名的原因,比如北京有个朝阳区,长春市也有个朝阳区,在高德地图和天地图的行政区划查询参数中,必须指定朝阳区关键字,而不支持北京市朝阳区这种,要么就是指定唯一地区编码的方式查询才是准确的,之前是按照名字作为关键字模糊查找,于是需要一个地名和编码的对照表,现在AI超级发达,直接叫他生成一个就可以,或者直接从天地图官网下载。
之前下载不正确还有一个
问题,那就是省市县行政区划的对照表信息陈旧,大概是二三十年前的,毕竟每年都可能有更新,很多地方是撤县并区,比如卢湾区就合并到了黄浦区,有些是县变成了区,比如崇明县就变成了崇明区,旧的地名就很可能找不到或者找不准。之前对照表是通过一个json文件来读取解析的,这个其实性能较低,现在从天地图官网下载的,直接转csv格式,二维
数据表格,性能最佳,而且还带了唯一编码,这下就好办多了,每个省市县都有对应的唯一编码,找到后传入这个编码查询就肯定是准的了。
之前还存在一个问题就是只提供了县的下载,并不支持指定某个省或者某个省下面的某个区这种,索性这次一步到位,做的完善,提供的省市县下拉框后面加个单选框,当单选框选中的省,则只下载下拉框中选择的省,同理市县都是这个机制,同时还支持文本框中输入地名模糊查找,这样可以快速的填入。全部完成试下来,效果非常棒。
## 二、效果图

## 三、相关代码
```cpp
#include "frmmapboundary.h"
#include "ui_frmmapboundary.h"
#include "qthelper.h"
#include "webview.h"
#include "maphelper.h"
#include "cityhelper.h"
frmMapBoundary::frmMapBoundary(QWidget *parent) : QWidget(parent), ui(new Ui::frmMapBoundary)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmMapBoundary::~frmMapBoundary()
{
on_btnStop_clicked();
delete ui;
}
void frmMapBoundary::showEvent(QShowEvent *)
{
//只需要加载一次/避免重复初始化
static bool isLoad = false;
if (!isLoad) {
isLoad = true;
this->initTable();
QMetaObject::invokeMethod(this, "loadMap", Qt::QueuedConnection);
}
}
void frmMapBoundary::initForm()
{
//设置右侧固定宽度
ui->widgetRight->setFixedWidth(AppData::RightWidth);
indexMain = indexSub = 0;
ui->tabWidget->setCurrentIndex(0);
//下载定时器
timerDown = new QTimer(this);
connect(timerDown, SIGNAL(timeout()), this, SLOT(getBoundary()));
timerDown->setInterval(AppConfig::BoundaryInterval);
//实例化浏览器控件并加入到布局
mapObj = NULL;
webView = new WebView(this);
webView->setLayout(ui->gridLayout);
//关联浏览器控件信号
connect(webView, SIGNAL(receiveDataFromJs(
QString, QVariant)), this, SLOT(receiveDataFromJs(QString, QVariant)));
}
void frmMapBoundary::initConfig()
{
MapHelper::loadMapCore(ui->cboxMapCore, AppConfig::BoundaryCore);
connect(ui->cboxMapCore, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
connect(ui->cboxMapCore, SIGNAL(currentIndexChanged(int)), this, SLOT(loadMap()));
connect(ui->cboxMapCore, SIGNAL(currentIndexChanged(int)), this, SLOT(initTable()));
QStringList listInterval;
listInterval << "500" << "1000" << "2000" << "5000";
ui->cboxInterval->addItems(listInterval);
ui->cboxInterval->setCurrentIndex(ui->cboxInterval->findText(QString::number(AppConfig::BoundaryInterval)));
connect(ui->cboxInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->cboxFileType->setCurrentIndex(AppConfig::BoundaryFileType);
connect(ui->cboxFileType, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
}
void frmMapBoundary::saveConfig()
{
AppConfig::BoundaryCore = ui->cboxMapCore->itemData(ui->cboxMapCore->currentIndex()).toInt();
AppConfig::BoundaryInterval = ui->cboxInterval->currentText().toInt();
AppConfig::BoundaryFileType = ui->cboxFileType->currentIndex();
AppConfig::writeConfig();
}
void frmMapBoundary::initTable()
{
cks.clear();
labs.clear();
bars.clear();
flags.clear();
ui->tableWidget->setRowCount(0);
//添加省份到下拉框/市区和县城会自动联动
QList<int> ids;
QStringList province = CityHelper::getProvince(&ids);
int count = ids.count();
for (int i = 0; i < count; ++i) {
ui->cboxProvince->addItem(province.at(i), ids.at(i));
}
QList<QString> columnNames;
QList<int> columnWidths;
columnNames << "区域" << "数量" << "进度";
columnWidths << 80 << 40 << 30;
int columnCount = columnNames.count();
ui->tableWidget->setColumnCount(columnCount);
ui->tableWidget->setHorizontalHeaderLabels(columnNames);
for (int i = 0; i < columnCount; ++i) {
ui->tableWidget->setColumnWidth(i, columnWidths.at(i));
}
QtHelper::initTableView(ui->tableWidget, AppData::RowHeight);
ui->tableWidget->setStyleSheet("QCheckBox{padding:0px 0px 0px 6px;}");
//启动计时
QElapsedTimer time;
time.start();
ui->tableWidget->setRowCount(count);
MapCore mapCore = (MapCore)ui->cboxMapCore->itemData(ui->cboxMapCore->currentIndex()).toInt();
//加载省市县
for (int index = 0; index < count; index++) {
//计算当前省份下有多少个县
QStringList list;
int id = ids.at(index);
QString provinceName = province.at(index);
QStringList city = CityHelper::getCity(provinceName);
if (provinceName.startsWith("香港") || provinceName.startsWith("澳门") || provinceName.startsWith("台湾")) {
if (MapHelper::isMapBaiDu(mapCore)) {
list << provinceName.left(2);
} else {
list << QString::number(id);
}
} else {
foreach (QString cityName, city) {
QList<int> ids;
QStringList county = CityHelper::getCounty(provinceName, cityName, &ids);
int count = ids.count();
for (int j = 0; j < count; ++j) {
int id = ids.at(j);
QString countyName = county.at(j);
if (MapHelper::isMapBaiDu(mapCore)) {
list << CityHelper::getAddr(provinceName, cityName, countyName);
} else {
list << QString::number(id);
}
}
}
}
//查询对应的唯一标识/高德和天地图用的唯一编码/百度地图用的地名
flags << list;
QCheckBox *ck = new QCheckBox;
ck->setText(provinceName);
cks << ck;
ui->tableWidget->setCellWidget(index, 0, ck);
QLabel *lab = new QLabel;
lab->setAlignment(Qt::AlignCenter);
labs << lab;
ui->tableWidget->setCellWidget(index, 1, lab);
QProgressBar *bar = new QProgressBar;
bar->setAlignment(Qt::AlignCenter);
//bar->setFormat("%v");
bars << bar;
ui->tableWidget->setCellWidget(index, 2, bar);
//显示对应的瓦片总数/设置进度条参数/并更新对应的值
int count = list.count();
if (count > 0) {
bars.at(index)->setRange(0, count);
}
bars.at(index)->setValue(0);
labs.at(index)->setText(QString::number(count));
}
//qDebug() << time.elapsed() << flags;
}
void frmMapBoundary::loadMap()
{
//根据不同地图内核实例化地图类
MapCore mapCore = (MapCore)ui->cboxMapCore->itemData(ui->cboxMapCore->currentIndex()).toInt();
MapHelper::initMapObj(this, &mapObj, mapCore);
mapObj->setWebView(webView);
mapObj->setSaveFile(SaveFile);
mapObj->load();
}
void frmMapBoundary::getBoundary()
{
//判断当前索引对应的省会的复选框有
没有勾选/没有勾选则找到下一个勾选的
int count = flags.count();
while (indexMain < count - 1 && !cks.at(indexMain)->isChecked()) {
indexMain++;
indexSub = 0;
}
//到了最后一个省会的索引停止定时器
if (indexMain == count || !cks.at(indexMain)->isChecked()) {
on_btnStop_clicked();
return;
}
//下载到省会的最后一个县/索引自动跳到下一个省会
QStringList list = flags.at(indexMain);
if (indexSub == list.count()) {
indexMain++;
indexSub = 0;
this->getBoundary();
return;
}
QString flag = list.at(indexSub);
this->searchBoundary(flag);
indexSub++;
//更新进度条
int value = bars.at(indexMain)->value();
bars.at(indexMain)->setValue(value + 1);
}
void frmMapBoundary::searchBoundary(const QString &name)
{
//高德地图和天地图需要找到编码对应的完整地址
currentName = name;
MapCore mapCore = (MapCore)ui->cboxMapCore->itemData(ui->cboxMapCore->currentIndex()).toInt();
if (mapCore == MapCore_GaoDe || mapCore == MapCore_Tian) {
currentName = CityHelper::getAddr(name);
}
ui->txtTip->setText(currentName);
QString js = QString("searchBoundary('%1', true)").arg(name);
this->runJs(js);
}
void frmMapBoundary::saveBoundary(QString &fileName, const QVariant &data)
{
//传过来的是行政区划的边界点坐标集合/存储到
文件 //可能会有多个数据/比如台湾的金门岛/都是独立的区域形状/存入数组
QString boundary;
QString type = ui->cboxFileType->currentText();
if (type == "文本文件") {
fileName.replace(".js", ".txt");
boundary = data.toString();
} else {
QStringList boundarys;
QStringList list = data.toString().split("|");
foreach (QString points, list) {
boundarys << QString("{'points': '%1'}").arg(points);
}
boundary = QString("var boundarys = [%1]").arg(boundarys.join(", "));
}
//写入到文件
MapHelper::writeFile(fileName, boundary);
}
void frmMapBoundary::clear()
{
//先进度条全部置为0
indexMain = indexSub = 0;
ui->txtTip->setText("等待下载...");
foreach (QProgressBar *bar, bars) {
bar->setValue(0);
}
}
void frmMapBoundary::runJs(const QString &js)
{
mapObj->runJs(js);
}
void frmMapBoundary::receiveDataFromJs(const QString &type, const QVariant &data)
{
//boundary类型是通过执行searchBoundary函数返回的
//newboundary类型是通过执行getBoundary函数返回的/也就是单击获取边界
按钮触发的
QString boundaryPath = AppConfig::BoundaryPath;
if (type == "boundary") {
//开启了定时器则按照目录存放
QString fileName = QString("%1/%2.js").arg(boundaryPath).arg(currentName);
if (timerDown->isActive()) {
QString provinceName = cks.at(indexMain)->text();
QString path = QString("%1/%2").arg(boundaryPath).arg(provinceName);
fileName = QString("%1/%2.js").arg(path).arg(currentName);
}
this->saveBoundary(fileName, data);
} else if (type == "newboundary") {
//单击获取边界按钮触发返回的数据
QString fileName = ui->cboxName->lineEdit()->text().trimmed();
fileName = QString("%1/%2.js").arg(boundaryPath).arg(fileName);
this->saveBoundary(fileName, data);
QtHelper::showMessageBoxInfo(QString("新边界保存成功, \n保存位置 %1").arg(fileName));
}
}
void frmMapBoundary::on_cboxProvince_currentIndexChanged(int)
{
QList<int> ids;
QString province = ui->cboxProvince->currentText();
QStringList names = CityHelper::getCity(province, &ids);
//同时添加唯一编码
int count = ids.count();
ui->cboxCity->clear();
for (int i = 0; i < count; ++i) {
ui->cboxCity->addItem(names.at(i), ids.at(i));
}
}
void frmMapBoundary::on_cboxCity_currentIndexChanged(int)
{
QList<int> ids;
QString province = ui->cboxProvince->currentText();
QString city = ui->cboxCity->currentText();
QStringList names = CityHelper::getCounty(province, city, &ids);
//同时添加唯一编码
int count = ids.count();
ui->cboxCounty->clear();
for (int i = 0; i < count; ++i) {
ui->cboxCounty->addItem(names.at(i), ids.at(i));
}
}
void frmMapBoundary::on_cboxCounty_currentIndexChanged(int)
{
QString province = ui->cboxProvince->currentText();
QString city = ui->cboxCity->currentText();
QString county = ui->cboxCounty->currentText();
QString name = CityHelper::getAddr(province, city, county);
ui->cboxName->lineEdit()->setText(name);
}
void frmMapBoundary::on_cboxName_currentIndexChanged(int)
{
if (this->isVisible()) {
on_btnLoad_clicked();
}
}
void frmMapBoundary::load(const QString &province, QString name, QString id)
{
//部分地方就只搞一个区域
MapCore mapCore = (MapCore)ui->cboxMapCore->itemData(ui->cboxMapCore->currentIndex()).toInt();
if (name != id && (province.startsWith("香港") || province.startsWith("澳门") || province.startsWith("台湾"))) {
if (MapHelper::isMapBaiDu(mapCore)) {
name = province.left(2);
} else {
id = id.left(6) + "000";
}
}
//高德地图和天地图支持精准的唯一编码查询
if (mapCore == MapCore_GaoDe || mapCore == MapCore_Tian) {
name = id;
}
//QtHelper::showMessageBoxInfo(name);
this->searchBoundary(name);
}
void frmMapBoundary::on_btnLoad_clicked()
{
QString province = ui->cboxProvince->currentText();
QString city = ui->cboxCity->currentText();
QString county = ui->cboxCounty->currentText();
//如果勾选的省则只下载省/勾选的市则下载对应市
QString name, id;
if (ui->rbtnProvince->isChecked()) {
name = province;
id = ui->cboxProvince->itemData(ui->cboxProvince->currentIndex()).toString();
} else if (ui->rbtnCity->isChecked()) {
name = CityHelper::getAddr(province, city, "");
id = ui->cboxCity->itemData(ui->cboxCity->currentIndex()).toString();
} else if (ui->rbtnCounty->isChecked()) {
name = CityHelper::getAddr(province, city, county);
id = ui->cboxCounty->itemData(ui->cboxCounty->currentIndex()).toString();
} else if (ui->rbtnName->isChecked()) {
name = ui->cboxName->currentText();
id = name;
}
ui->cboxName->lineEdit()->setText(name);
this->load(province, name, id);
}
void frmMapBoundary::on_btnSelectAll_clicked()
{
foreach (QCheckBox *ck, cks) {
ck->setChecked(true);
}
}
void frmMapBoundary::on_btnSelectNone_clicked()
{
foreach (QCheckBox *ck, cks) {
ck->setChecked(false);
}
}
void frmMapBoundary::on_btnEdit_clicked()
{
if (ui->btnEdit->text() == "编辑") {
this->runJs("editPolygon(true)");
ui->btnEdit->setText("结束");
} else {
this->runJs("editPolygon(false)");
ui->btnEdit->setText("编辑");
}
}
void frmMapBoundary::on_btnBoundary_clicked()
{
this->runJs("getBoundary()");
}
void frmMapBoundary::on_btnStart_clicked()
{
#ifdef betaversion
QtHelper::showMessageBoxError("正式版购买源码
编译才能使用, 不好意思!");
return;
#endif
//没有选中一个则不用处理
bool exist = false;
foreach (QCheckBox *ck, cks) {
if (ck->isChecked()) {
exist = true;
break;
}
}
if (!exist) {
return;
}
if (!timerDown->isActive()) {
timerDown->start(ui->cboxInterval->currentText().toInt());
this->clear();
}
}
void frmMapBoundary::on_btnStop_clicked()
{
if (timerDown->isActive()) {
timerDown->stop();
this->clear();
}
}
void frmMapBoundary::on_tableWidget_cellPressed(int row, int column)
{
//选中行切换复选框
if (row >= 0) {
QCheckBox *cbox = cks.at(row);
cbox->setCheckState(cbox->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
}
}
```
## 四、相关地址
1. 国内站点:[
https://gitee.com/feiyangqingyun](https://gitee.com/feiyangqingyun)
2. 国际站点:[
https://github.com/feiyangqingyun](https://github.com/feiyangqingyun)
3. 个人作品:[
https://blog.csdn.net/feiyangqingyun/article/details/97565652](https://blog.csdn.net/feiyangqingyun/article/details/97565652)
4. 文件地址:[
https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A](https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A) 提取码:o05q 文件名:bin_map.zip
## 五、功能特点
### 5.1 地图功能
1. 支持多种地图内核,默认采用百度地图,可选高德地图、天地图、腾讯地图、谷歌地图等。
2. 同时支持在线地图和离线地图两种模式,离线地图方便在不联网的场景中使用。
3. 支持各种地图控件的启用,比如地图导航、地图类型、缩略图、比例尺、全景导航、实时路况、绘图工具、结果面板等。
4. 支持多种地图功能的动态启用禁用,比如地图拖曳、键盘操作、滚轮缩放、双击放大、连续缩放、地图测距等。
5. 提供众多js函数接口用于交互,参数极其丰富,能够想到的应用场景需求都有。
6. 统一的信号槽机制,地图中的结果统一信号发送出去,收到后根据type类型区分。
7. 支持地图交互,比如鼠标按下获取对应位置的经纬度。单击标注点弹出对应点的信息。
8. 支持添加标注、删除标注、移动标注、清空标注。
9. 标注点可以指定图标图片和尺寸,支持gif动图,支持指定以图片中心对齐还是底部中心对齐。可以设置旋转角度,带富文本提示信息。
10. 标注点事件支持单击发信号通知和自己弹框
显示信息。
11. 提供地址转坐标和坐标转地址接口。
12. 支持各种图形绘制,包括折线图、多边形、矩形、圆形、弧线等。
13. 可显示悬浮的绘图工具栏,直接在地图上划线、标注点、矩形、圆形等。
14. 支持各种区域搜索,比如矩形区域、圆形区域,可以按照关键字匹配将搜索结果显示在地图中。
15. 可动态添加离线的行政区边界点数据。可以搜索行政区划并获取该区域的边界点数据。数据可以保存到文件以便离线使用。
16. 支持点聚合功能,多个小标注点合并到一个大标注点,防止点密集导致交互不友好。
17. 可以添加海量点,每个点都可以单击获取对应坐标和信息。
18. 所有的覆盖物信息比如标注点、矩形、多边形、折线图等,都可以主动获取对应的信息比如坐标点和路径等。
19. 支持路径规划,支持公交路线、自驾路线、步行路线、骑行路线,不同查询支持不同策略,可选最少时间、最少换乘、不走高架等。
20. 路径规划结果可以显示在地图中,也可以获取到路径点坐标集合。这个数据可以保存到文件,以便发给机器人或者无人机做导航用来轨迹移动。
21. 可以设置不同的地图视图比如街道图、卫星图、混合图。
22. 可以设置不同的样式,比如午夜蓝、青草绿等样式风格。
23. 可以设置地图的旋转角度和倾斜角度。
24. 提供经纬度坐标纠偏转换功能,比如传入的GPS坐标需要转换到百度地图坐标或者高德地图坐标。各种坐标系转换全部离线函数,支持地球坐标系WGS-84、火星坐标系GCJ-02、百度坐标系BD-09之间的互相转换,涵盖了各种地图的坐标系。
25. 提供动态轨迹点移动功能,按照给定的经纬度坐标集合平滑移动。
26. 同时支持qwidget和qml,支持编译到安卓系统运行。
### 5.2 其他功能
1. 提供离线地图下载模块,可以选择不同的地图内核比如百度地图或者谷歌地图,不同的地图类型比如下载街道图还是卫星图,不同的地图层级,多线程极速下载。
2. 表格行实时显示对应的瓦片下载进度,有下载超时时间,重试次数,每个瓦片下载完成都发送信号通知,参数包括下载用时。
3. 提供省市轮廓图下载模块,自动下载各个地区的轮廓图,保存到脚本文件或者文本文件。
4. 支持手动调整不同区域的轮廓边界,调整后可以主动获取调整后的边界点集合。
5. 提供动态点位示例,手动在地图上选点并添加标注,附带自定义的信息比如速度和时间等。
6. 提供海量点位示例,批量添加标注点、点聚合、海量点。用于测试环境中支持的最大点位性能。
7. 提供动态轨迹示例,在地图上鼠标按下选择起点和终点后,查询路线,获取路径轨迹点,模拟轨迹平滑移动。可以筛选数据将过多的路径点筛选到设定的点数。
8. 提供轨迹回放示例,按照指定的轨迹点列表回放,也可以导入轨迹点数据进行回放。同时支持在街道图、卫星图、混合图中回放轨迹。
9. 提供省市区域地图示例,采用echart组件,同时支持闪烁点图、迁徙图、区域地图、世界地图、仪表盘等。可以设置标题、提示信息、背景颜色、文字颜色、线条颜色、区域颜色等各种颜色。
10. 省市区域地图示例,内置世界地图、全国地图、省份地图、地区地图,可以精确到县,所有地图全部离线使用。可设置城市的名称、值、经纬度集合。
11. 内置通用浏览器组件,同时支持webkit/webengine/miniblink等内核。提供网页控件示例,演示打开网页和本地网页文件。
12. 支持任意Qt版本、任意系统、任意编译器。