Honor mobile map package expiration date

View inC++QMLView on GitHubSample viewer app

Access the expiration information of an expired mobile map package.

screenshot

Use case

The data contained within a mobile map package (MMPK) may only be relevant for a fixed period of time. Using ArcGIS Pro, the author of an MMPK can set an expiration date to ensure the user is aware the data is out of date.

As long as the author of an MMPK has set an expiration date, the expiration date can be read even if the MMPK has not yet expired. For example, developers could also use this API to warn app users that an MMPK may be expiring soon.

How to use the sample

Load the app. The author of the MMPK used in this sample chose to set the MMPK's map as still readable, even if it's expired. The app presents expiration information to the user.

How it works

  1. Create a MobileMapPackage passing in the path to the mobile map package's location on the device.
  2. Load the mobile map package.
  3. Present Expiration information to the user with the Expiration::message() and Expiration::dateTime() getters, which are set by the map author.

Relevant API

  • Expiration
  • MobileMapPackage

Offline Data

Read more about how to set up the sample's offline data here.

Link Local Location
Lothian Rivers Anno MMPK <sdcard>/ArcGIS/Samples/MapPackage/LothianRiversAnno.mmpk

Tags

expiration, mmpk

Sample Code

HonorMobileMapPackageExpiration.cppHonorMobileMapPackageExpiration.cppHonorMobileMapPackageExpiration.hHonorMobileMapPackageExpiration.qml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// [WriteFile Name=HonorMobileMapPackageExpiration, Category=Maps]
// [Legal]
// Copyright 2019 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "HonorMobileMapPackageExpiration.h"

#include "MobileMapPackage.h"
#include "MapQuickView.h"

#include <QtGlobal>
#include <QDir>

#ifdef Q_OS_IOS
#include <QStandardPaths>
#endif // Q_OS_IOS

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace {
QString defaultDataPath()
{
  QString dataPath;

#ifdef Q_OS_ANDROID
  dataPath = "/sdcard";
#elif defined Q_OS_IOS
  dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
  dataPath = QDir::homePath();
#endif

  return dataPath;
}
}

// sample MMPK location
const QString sampleMmpk {"/ArcGIS/Runtime/Data/mmpk/LothianRiversAnno.mmpk"};

HonorMobileMapPackageExpiration::HonorMobileMapPackageExpiration(QObject* parent /* = nullptr */):
  QObject(parent),
  m_dataPath(defaultDataPath() + sampleMmpk)
{
  // connect to the Mobile Map Package instance to know when errors occur
  connect(MobileMapPackage::instance(), &MobileMapPackage::errorOccurred,
          [](Error e)
  {
    if (e.isEmpty())
    {
      return;
    }

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

}

HonorMobileMapPackageExpiration::~HonorMobileMapPackageExpiration() = default;

void HonorMobileMapPackageExpiration::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<HonorMobileMapPackageExpiration>("Esri.Samples", 1, 0, "HonorMobileMapPackageExpirationSample");
}

MapQuickView* HonorMobileMapPackageExpiration::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void HonorMobileMapPackageExpiration::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;

  emit mapViewChanged();

  createMapPackage(m_dataPath);
}

void HonorMobileMapPackageExpiration::createMapPackage(const QString& path)
{
  // 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())
    {
      return;
    }

    // Access Expiration Info
    Expiration expiration = m_mobileMapPackage->expiration();
    if (!expiration.isEmpty())
    {
      if (expiration.isExpired())
      {
        const QString message = expiration.message();
        const QString date = expiration.dateTime().toString(Qt::ISODate).split("T")[0];
        const QString time = expiration.dateTime().time().toString();
        m_expirationString = QString("%1\nExpired on %2 at %3 UTC").arg(message, date, time);
        emit expirationStringChanged();

        // return if access after expiration is not allowed
        if (expiration.type() == ExpirationType::PreventExpiredAccess)
          return;
      }
    }

    // set the map view's map to the first map in the mobile map package
    m_mapView->setMap(m_mobileMapPackage->maps().at(0));
  });

  m_mobileMapPackage->load();
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.