Start the Local Server and Local Map Service, create an ArcGIS Map Image Layer from the Local Map Service, and add it to a map.
      
  
    
Use case
For executing offline geoprocessing tasks in your apps via an offline (local) server.
How to use the sample
The Local Server and local map service will automatically be started and, once running, a map image layer will be created and added to the map.
How it works
- Create and run the Local Server.
LocalServer::instancecreates the Local Server.LocalServer::start()starts the server asynchronously.
 - Wait for server to be in the  
LocalServerStatus::Startedstate.LocalServer::statusChanged()fires whenever the running status of the Local Server changes.
 - Create and run a local service, example of running a 
LocalMapService.new LocalMapService(Url), creates a local map service with the given URL path to the map package (mpkxfile).LocalMapService::start()starts the service asynchronously.- The service is added to the Local Server automatically.
 
 - Wait for map service to be in the 
LocalServerStatus::Startedstate.LocalMapService::statusChanged()signal fires whenever the status of the local service has changed.
 - Create an ArcGIS map image layer from local map service.
- Create a 
ArcGISMapImageLayer(Url)from local map service URL,LocalMapService::url() - Add the layer to the map's operational layers.
 - Connect to the map image layer's 
LoadStatusChangedsignal. - When the layer's status is 
Loaded, set the map view's extent to the layer's full extent. 
 - Create a 
 
Relevant API
- ArcGISMapImageLayer
 - LocalMapService
 - LocalServer
 - LocalServerStatus
 
Offline data
Read more about how to set up the sample's offline data here.
| Link | Local Location | 
|---|---|
| RelationshipID map package | <userhome>/ArcGIS/Runtime/Data/mpkx/RelationshipID.mpkx | 
Additional information
Local Server can be downloaded for Windows and Linux platforms. Local Server is not supported on macOS.
Tags
image, layer, local, offline, server
Sample Code
// [WriteFile Name=LocalServerMapImageLayer, Category=LocalServer]
// [Legal]
// Copyright 2017 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
// sample headers
#include "LocalServerMapImageLayer.h"
// ArcGIS Maps SDK headers
#include "ArcGISMapImageLayer.h"
#include "Basemap.h"
#include "Envelope.h"
#include "LayerListModel.h"
#include "LocalMapService.h"
#include "LocalServer.h"
#include "LocalServerTypes.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "MapViewTypes.h"
#include "Viewpoint.h"
// Qt headers
#include <QDir>
#include <QFuture>
#include <QTemporaryDir>
using namespace Esri::ArcGISRuntime;
LocalServerMapImageLayer::LocalServerMapImageLayer(QQuickItem* parent) :
  QQuickItem(parent)
{
  // Create a temporary directory for the local server if one has not already been created
  if (!LocalServer::appDataPath().isEmpty() && !LocalServer::tempDataPath().isEmpty())
    return;
  // create temp/data path
  const QString tempPath = LocalServerMapImageLayer::shortestTempPath() + "/EsriQtTemp";
  // create the directory
  m_tempDir = std::make_unique<QTemporaryDir>(tempPath);
  // set the temp & app data path for the local server
  LocalServer::instance()->setTempDataPath(m_tempDir->path());
  LocalServer::instance()->setAppDataPath(m_tempDir->path());
}
LocalServerMapImageLayer::~LocalServerMapImageLayer() = default;
void LocalServerMapImageLayer::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<LocalServerMapImageLayer>("Esri.Samples", 1, 0, "LocalServerMapImageLayerSample");
}
void LocalServerMapImageLayer::componentComplete()
{
  QQuickItem::componentComplete();
  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);
  // Create a map using the topographic BaseMap
  m_map = new Map(BasemapStyle::ArcGISTopographic, this);
  // Set map to map view
  m_mapView->setMap(m_map);
  QString dataPath = QDir::homePath() + "/ArcGIS/Runtime/Data";
  // create a map service
  m_localMapService = new LocalMapService(dataPath + "/mpkx/RelationshipID.mpkx", this);
  if (LocalServer::instance()->isInstallValid())
  {
    connectSignals();
    if (LocalServer::status() == LocalServerStatus::Started)
      m_localMapService->start();
    else
      LocalServer::start();
  }
}
void LocalServerMapImageLayer::connectSignals()
{
  // local server status
  connect(LocalServer::instance(), &LocalServer::statusChanged, this, [this]()
  {
    if (LocalServer::status() == LocalServerStatus::Started)
    {
      // start the service
      m_localMapService->start();
    }
  });
  // local map service status
  connect(m_localMapService, &LocalMapService::statusChanged, this, [this]()
  {
    if (m_localMapService->status() == LocalServerStatus::Started)
    {
      ArcGISMapImageLayer* mapImageLayer = new ArcGISMapImageLayer(QUrl(m_localMapService->url()), this);
      connect(mapImageLayer, &ArcGISMapImageLayer::loadStatusChanged, this, [this, mapImageLayer](LoadStatus status)
      {
        if (status == LoadStatus::Loaded)
        {
          m_mapView->setViewpointAsync(Viewpoint(mapImageLayer->fullExtent()));
        }
      });
      m_map->operationalLayers()->append(mapImageLayer);
    }
  });
}
QString LocalServerMapImageLayer::shortestTempPath()
{
  // get tmp and home paths
  const QString tmpPath = QDir::tempPath();
  const QString homePath = QDir::homePath();
  // return whichever is shorter, temp or home path
  if (homePath.length() > tmpPath.length())
    return tmpPath;
  else
    return homePath;
}