Arcgis runtime sdk for Qt 开发记录(二)离线地图的加载

5 篇文章 13 订阅

前言

Arcgis for Qt的sdk中支持多种格式的地图文件加载,常用的是tpk,tpkx和mmpk这几种地图包。如何导出地图包,可以参考另一篇文章使用Arcgis Pro 2.5生成地图文件(tpk,mmpk)_dinjay的博客-CSDN博客

导入tpk作为地图底图

先创建一个arcgis widget工程,添加以下代码:


// Copyright 2019 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See the Sample code usage restrictions document for further information.
//

// C++ API headers
#include "Basemap.h"

// Other headers
#include "Arcgis_test_100_8.h"

#include "Map.h"
#include "MapGraphicsView.h"

#include <ArcGISTiledLayer.h>
#include <QCoreApplication>
#include <TileCache.h>

using namespace Esri::ArcGISRuntime;

Arcgis_test_100_8::Arcgis_test_100_8(QWidget* parent /*=nullptr*/):
    QMainWindow(parent)
{

    // Create the Widget view
    m_mapView = new MapGraphicsView(this);

    // set the mapView as the central widget
    setCentralWidget(m_mapView);

    QString path = QCoreApplication::applicationDirPath()+"/testExport.tpk";
    // 创建切片图层
    TileCache* tileCache = new TileCache(path, this);
    ArcGISTiledLayer* tiledLayer = new ArcGISTiledLayer(tileCache, this);
    // 将图层关联到底图上
    Basemap *basemap = new Basemap(tiledLayer, this);
    m_map = new Map(basemap, this);
    
    // 监听底图加载完成事件
    connect(m_map, &Map::doneLoading, this, [this](Error e){
        if (!e.isEmpty())
        {
            qDebug()<<"load map end code:"<<e.code()<<" msg:"<<e.message();
        }
        else
        {
            qDebug()<<"[lib] load map successed!";
        }
    });

    // 设置到mapview上
    m_mapView->setMap(m_map);
    m_mapView->setAttributionTextVisible(false);
}

// destructor
Arcgis_test_100_8::~Arcgis_test_100_8()
{
}


导入之前生成的tpk文件,编译运行。

 下面这个setAttributionTextVisible是把mapview默认的底部文字隐藏掉,

m_mapView->setAttributionTextVisible(false);

如果我们不设置的话,在底部会有一行状态栏,如下图:

导入MMPK作为地图底图

添加一个新函数loadMMPK,将之前生成的mmpk文件导入加载。

void Arcgis_test_100_8::loadMMPK()
{
    QString path = QCoreApplication::applicationDirPath()+"/testExport.mmpk";
    connect(MobileMapPackage::instance(), &MobileMapPackage::errorOccurred,
              [](Error e)
      {
        if (e.isEmpty())
        {
          return;
        }

        qDebug() << QString("Error: %1 %2").arg(e.message(), e.additionalMessage());
      });

  //! [open mobile map package cpp snippet]
  // instatiate a mobile map package
  m_mobileMapPackage = new MobileMapPackage(path, this);

  // wait for the mobile map package to load
  connect(m_mobileMapPackage, &MobileMapPackage::doneLoading, this, [this](Error error)
  {
    if (!error.isEmpty())
    {
      qDebug() << QString("Package load error: %1 %2").arg(error.message(), error.additionalMessage());
      return;
    }

    if (!m_mobileMapPackage || !m_mapView || m_mobileMapPackage->maps().isEmpty())
    {
        qDebug()<<"err...";
      return;
    }

    // The package contains a list of maps that could be shown in the UI for selection.
    // For simplicity, obtain the first map in the list of maps.
    // set the map on the map view to display
    m_mapView->setMap(m_mobileMapPackage->maps().at(0));
    qDebug()<<"ok...";
  });

  m_mobileMapPackage->load();
  //! [open mobile map package cpp snippet]
}

mmpk是移动地图包,顾名思义主要是给android或者ios移动端app上使用的,包含了地图要素和地理数据信息,以便进行各种分析,也能为路径规划使用。

代码可以去网盘上下载:

链接:https://pan.baidu.com/s/1siC5Cs75Sd89TyVvLs5aiw 
提取码:bndt 
 

离线地图包的加载这里就讲完了,比较简单,下一篇文章介绍地图操作的基本接口使用

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值