Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

WMTS layer

This code sample is available for these platforms:
View Sample on GitHub

Display a layer from a Web Map Tile Service.

screenshot

Use case

WMTS services can have several layers. You can use Runtime to explore the layers available from a service. This would commonly be used to enable a browsing experience where users can choose which layers they want to display at run time.

How to use the sample

The layer will be displayed automatically. Use the buttons to choose a different method of loading the layer.

How it works

To display a WMTS layer directly from a URL:

  1. Create a WmtsService object using the URL of the WMTS service.
  2. Create a WmtsLayer object with the ID of the layer to display.

To explore layers from a WMTS service:

  1. Create a WmtsService object using the URL of the WMTS service.
  2. After loading the WMTS service, get the list of WmtsLayerInfo objects from the service info of the WMTS service.
  3. Use one of the layer infos to create the WMTS layer.
  4. Create a basemap with the WMTS layer and set it to the map.

Relevant API

  • WmtsLayer
  • WmtsLayerInfo
  • WmtsService
  • WmtsServiceInfo

About the data

The map visualizes world time zones.

Tags

OGC, WmtsLayer, WmtsLayerInfo, WmtsService, WmtsServiceInfo, layer, raster, tiled, web map tile service

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.WMTSLayer.WMTSLayer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:esri="using:Esri.ArcGISRuntime"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <StackPanel>
                <Button x:Name="UriButton"
                        Click="UriButton_Click"
                        Margin="10"
                        HorizontalAlignment="Stretch"
                        IsEnabled ="false"
                        Content="WMTSLayer via Uri" />
                <Button x:Name="InfoButton"
                        Click="InfoButton_Click"
                        HorizontalAlignment="Stretch"
                        Margin="10"
                        Content="WMTSLayer via WmtsLayerInfo" />
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
// 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.

using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Ogc;
using System;
using System.Collections.Generic;
using Windows.UI.Popups;
using Windows.UI.Xaml;

namespace ArcGISRuntime.UWP.Samples.WMTSLayer
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "WMTS layer",
        "Layers",
        "This sample demonstrates how to display a WMTS layer on a map via a Uri and WmtsLayerInfo.",
        "")]
    public partial class WMTSLayer
    {


        public WMTSLayer()
        {
            InitializeComponent();
            LoadWMTSLayer(true);
        }

        private void UriButton_Click(object sender, RoutedEventArgs e)
        {
            //Load the WMTS layer using Uri method.
            LoadWMTSLayer(true);

            // Disable and enable the appropriate buttons.
            UriButton.IsEnabled = false;
            InfoButton.IsEnabled = true;
        }

        private void InfoButton_Click(object sender, RoutedEventArgs e)
        {
            //Load the WMTS layer using layer info.
            LoadWMTSLayer(false);

            // Disable and enable the appropriate buttons.
            UriButton.IsEnabled = true;
            InfoButton.IsEnabled = false;
        }

        private async void LoadWMTSLayer(bool uriMode)
        {
            try
            {
                // Create a new map.
                Map myMap = new Map();

                // Get the basemap from the map.
                Basemap myBasemap = myMap.Basemap;

                // Get the layer collection for the base layers.
                LayerCollection myLayerCollection = myBasemap.BaseLayers;

                // Create an instance for the WMTS layer.
                WmtsLayer myWmtsLayer;

                // Define the Uri to the WMTS service.
                Uri wmtsUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer/WMTS");

                if (uriMode)
                {
                    // Create a WMTS layer using a Uri and provide an Id value.
                    myWmtsLayer = new WmtsLayer(wmtsUri, "WorldTimeZones");

                }
                else
                {
                    // Define a new instance of the WMTS service.
                    WmtsService myWmtsService = new WmtsService(wmtsUri);

                    // Load the WMTS service.
                    await myWmtsService.LoadAsync();

                    // Get the service information (i.e. metadata) about the WMTS service.
                    WmtsServiceInfo myWmtsServiceInfo = myWmtsService.ServiceInfo;

                    // Obtain the read only list of WMTS layer info objects.
                    IReadOnlyList<WmtsLayerInfo> myWmtsLayerInfos = myWmtsServiceInfo.LayerInfos;

                    // Create a WMTS layer using the first item in the read only list of WMTS layer info objects.
                    myWmtsLayer = new WmtsLayer(myWmtsLayerInfos[0]);
                }

                // Add the WMTS layer to the layer collection of the map.
                myLayerCollection.Add(myWmtsLayer);

                // Assign the map to the MapView.
                MyMapView.Map = myMap;
            }
            catch (Exception ex)
            {
                MessageDialog messageDlg = new MessageDialog(ex.ToString(), "Error");
                await messageDlg.ShowAsync();
            }
        }
    }
}