Show organization basemaps

Loading

Code

#include "AuthenticationManager.h"
#include "Map.h"
#include "MapQuickView.h"
#include "Basemap.h"
#include "BasemapListModel.h"
#include "Portal.h"
#include "ShowOrgBasemaps.h"

using namespace Esri::ArcGISRuntime;

ShowOrgBasemaps::ShowOrgBasemaps(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent),
  m_portal(new Portal(this))
{
  AuthenticationManager::instance()->setCredentialCacheEnabled(false);
}

ShowOrgBasemaps::~ShowOrgBasemaps()
{
}

void ShowOrgBasemaps::init()
{
  qmlRegisterUncreatableType<AuthenticationManager>("Esri.Samples", 1, 0, "AuthenticationManager", "AuthenticationManager is uncreateable");
  qmlRegisterUncreatableType<QAbstractListModel>("Esri.Samples", 1, 0, "AbstractListModel", "AbstractListModel is uncreateable");
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<ShowOrgBasemaps>("Esri.Samples", 1, 0, "ShowOrgBasemapsSample");
}


void ShowOrgBasemaps::componentComplete()
{
  QQuickItem::componentComplete();

  if (m_portal)
  {
    connect(m_portal, &Portal::loadStatusChanged, this, [this]()
    {
      m_portalLoaded = m_portal->loadStatus() == LoadStatus::Loaded;

      emit portalLoadedChanged();
      emit orgNameChanged();

      if (m_portalLoaded)
        m_portal->fetchBasemaps();
    });

    connect(m_portal, &Portal::basemapsChanged, this, [this]()
    {
      emit basemapsChanged();
    });
  }

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  if (m_mapView)
    m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);

  emit authManagerChanged();
}

bool ShowOrgBasemaps::portalLoaded() const
{
  return m_portalLoaded;
}

QString ShowOrgBasemaps::orgName() const
{
  if (!m_portalLoaded || !m_portal || !m_portal->portalInfo())
    return QString();

  return m_portal->portalInfo()->organizationName();
}

QAbstractListModel* ShowOrgBasemaps::basemaps() const
{
  return m_portal ? m_portal->basemaps() : nullptr;
}

QString ShowOrgBasemaps::mapLoadError() const
{
  return m_mapLoadError;
}

void ShowOrgBasemaps::load(bool anonymous)
{
  if (!m_portal)
    return;

  if (anonymous)
    m_portal->load();
  else {
    Credential* cred = new Credential(OAuthClientInfo("W3hPKzPbeJ0tr8aj", OAuthMode::User), this);
    m_portal->setCredential(cred);
    m_portal->load();
  }
}

void ShowOrgBasemaps::loadSelectedBasemap(int index)
{
  if (!m_portal || !m_portal->basemaps())
    return;

  Basemap* selectedBasemap = m_portal->basemaps()->at(index);
  if (!selectedBasemap)
    return;

  if (m_map)
  {
    delete m_map;
    m_map = nullptr;
  }

  m_mapLoadError.clear();
  emit mapLoadErrorChanged();

  m_map = new Map(selectedBasemap, this);

  connect(m_map, &Map::errorOccurred, this, [this]()
  {
    m_mapLoadError = m_map->loadError().message();
    emit mapLoadErrorChanged();
  });

  connect(m_map, &Map::loadStatusChanged, this, [this]()
  {
    if (!m_map || m_map->loadStatus() != LoadStatus::Loaded)
      return;

    m_mapView->setMap(m_map);
    m_mapView->setVisible(true);
  });

  m_map->load();
}

void ShowOrgBasemaps::errorAccepted()
{
  m_mapLoadError.clear();
  emit mapLoadErrorChanged();
}

AuthenticationManager *ShowOrgBasemaps::authManager() const
{
  return AuthenticationManager::instance();
}


In this topic
  1. Code