Qt write map comprehensive application 52 - load offline map

1, Foreword

The loading method of offline map is almost the same as that of online map. The only biggest difference is that in the past, one js file may be imported, but now multiple local js files are required. Moreover, the version of js files circulating on the Internet is relatively old, which means that the new version of maps supporting opengl cannot be supported, Later, go online search to see if you can get the latest version.

Offline map loading depends on a pile of js files. The location of the entire folder can be customized. It is OK to specify the location when the web page code is introduced. It is generally recommended to create a new directory under the executable file for special storage, which is convenient for management. The image file directory corresponding to the offline map is also configurable, and it is also recommended to put it in this directory.

2, Functional features

  1. Multi thread synchronous download of multi-level tile map without card interface.
  2. Multiple offline map download request addresses are built in, and one sending request is automatically selected at random.
  3. The download map type supports both street map and satellite map.
  4. Automatically calculate the number of downloaded tiles in the visual area or administrative area.
  5. The download level can be customized and selected.
  6. When each tile is downloaded, the notification is sent.
  7. The maximum download timeout can be set. If it exceeds the timeout, it will be discarded and jump to the next download task.
  8. The download progress, the number of tiles downloaded at the current level and the total number of tiles are displayed in real time.
  9. The download can be stopped during the download process, and the total time will be counted automatically after the download is completed.
  10. Built in longitude and latitude and screen coordinate mutual conversion function.
  11. At present, baidu maps are supported. Other maps such as Google maps, Tencent maps and Gaode maps can be customized.
  12. The function interface is friendly and unified, simple and convenient to use, just one class.
  13. Support any Qt version, any system and any compiler.

3, Experience address

  1. Experience address: https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A Extraction code: o05q file name: bin_map.zip
  2. Domestic sites: https://gitee.com/feiyangqingyun
  3. International sites: https://github.com/feiyangqingyun
  4. Personal homepage: https://blog.csdn.net/feiyangqingyun
  5. Zhihu homepage: https://www.zhihu.com/people/feiyangqingyun/

4, Renderings

5, Related code

void frmMapDownload::getCount()
{
    //Calculate the number of tiles
    QString pointLeftBottom = ui->txtPointLeftBottom->text();
    QString pointRightTop = ui->txtPointRightTop->text();
    QStringList listLeftBottom = pointLeftBottom.split(",");
    QStringList listRightTop = pointRightTop.split(",");

    double lngLeftBottom = listLeftBottom.at(0).toDouble();
    double latLeftBottom = listLeftBottom.at(1).toDouble();
    double lngRightTop = listRightTop.at(0).toDouble();
    double latRightTop = listRightTop.at(1).toDouble();

    //mapType=0 means Baidu map = 4 means Google map
    int mapType = ui->cboxMapType->currentIndex();
    for (int zoom = indexMin; zoom <= indexMax; zoom++) {
        int index = zoom - indexMin;

        //Different maps calculate different coordinates
        QPoint pt1, pt2;
        if (mapType == 3) {
            pt1 = WebHelper::lngLatToTileTian(lngLeftBottom, latLeftBottom, zoom);
            pt2 = WebHelper::lngLatToTileTian(lngRightTop, latRightTop, zoom);
        } else if (mapType == 4) {
            pt1 = WebHelper::lngLatToTileGoogle(lngLeftBottom, latLeftBottom, zoom);
            pt2 = WebHelper::lngLatToTileGoogle(lngRightTop, latRightTop, zoom);
        } else {
            pt1 = WebHelper::lngLatToTileBaiDu(lngLeftBottom, latLeftBottom, zoom);
            pt2 = WebHelper::lngLatToTileBaiDu(lngRightTop, latRightTop, zoom);
        }

        //Calculate XY coordinate maximum and minimum
        int xmin = qMin(pt1.x(), pt2.x());
        int xmax = qMax(pt1.x(), pt2.x());
        int ymin = qMin(pt1.y(), pt2.y());
        int ymax = qMax(pt1.y(), pt2.y());
        pt1 = QPoint(xmin, ymin);
        pt2 = QPoint(xmax, ymax);

        //It is convenient to print and view the calculation results
        if (zoom == 20) {
            qDebug() << lngLeftBottom << latLeftBottom << lngRightTop << latRightTop << pt1 << pt2;
        }

        //Number of tiles at current level
        int count = 0;
        for (int j = xmin; j <= xmax; j++) {
            for (int k = ymin; k <= ymax; k++) {
                count++;
            }
        }

        //Display the corresponding total number of tiles, set the progress bar parameters, and update the corresponding values
        if (count > 0) {
            bars.at(index)->setRange(0, count);
        }

        bars.at(index)->setValue(0);
        labs.at(index)->setText(QString::number(count));
        pointLeftBottoms[index] = pt1;
        pointRightTops[index] = pt2;
    }
}

void frmMapDownload::clear()
{
    //All advance bars are set to 0
    currentCount = 0;
    foreach (QProgressBar *bar, bars) {
        bar->setValue(0);
    }
}

void frmMapDownload::receiveDataFromJs(const QString &type, const QVariant &data)
{
    if (data.isNull()) {
        return;
    }

    //qDebug() << "frmMapDownload" << type << data;
    QString result = data.toString();
    if (type == "zoom") {
        float zoom = result.toFloat();
        QString strZoom = QString::number(zoom, 'f', 3);
        ui->txtZoom->setText(strZoom);
    } else if (type == "bounds") {
        QStringList list = result.split(",");
        if (list.count() == 7) {
            QString lat, lng, point;
            lng = WebHelper::getLngLat1(list.at(0));
            lat = WebHelper::getLngLat1(list.at(1));
            point = QString("%1,%2").arg(lng).arg(lat);
            ui->txtPointLeftBottom->setText(point);

            lng = WebHelper::getLngLat1(list.at(2));
            lat = WebHelper::getLngLat1(list.at(3));
            point = QString("%1,%2").arg(lng).arg(lat);
            ui->txtPointRightTop->setText(point);

            lng = WebHelper::getLngLat1(list.at(4));
            lat = WebHelper::getLngLat1(list.at(5));
            point = QString("%1,%2").arg(lng).arg(lat);
            ui->txtPointCenter->setText(point);

            float zoom = list.at(6).toFloat();
            QString strZoom = QString::number(zoom, 'f', 3);
            ui->txtZoom->setText(strZoom);

            //Automatically count the number of tiles
            this->getCount();
            //Scroll bar scroll to the bottom, generally need to download a large level
            ui->tableWidget->scrollToBottom();
        }
    } else if (type == "point") {
        QString point = WebHelper::getLngLat2(result);
        ui->txtPointCenter->setText(point);
    }
}

Added by GreenMarch on Sat, 15 Jan 2022 06:58:50 +0200