Add WMTS layer

View on GitHub

Display a layer from a Web Map Tile Service.

Image of add WMTS layer

Use case

WMTS services can have several layers. You can use the ArcGIS Maps SDKs 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

layer, OGC, raster, tiled, web map tile service

Sample Code

add_wmts_layer.dart
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
// Copyright 2024 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
//
//   https://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.
//

import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/utils/sample_state_support.dart';
import 'package:flutter/material.dart';

class AddWmtsLayer extends StatefulWidget {
  const AddWmtsLayer({super.key});

  @override
  State<AddWmtsLayer> createState() => _AddWmtsLayerState();
}

class _AddWmtsLayerState extends State<AddWmtsLayer> with SampleStateSupport {
  // Create a controller for the map view.
  final _mapViewController = ArcGISMapView.createController();
  // The URI to a WMTS service.
  final wmtsServiceUri = Uri.parse(
    'https://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer/WMTS',
  );
  // A flag indicating which layer constructor is currently being used.
  var _fromUriActive = true;
  // A flag for when the map is ready and controls can be used.
  var _ready = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: false,
        child: Stack(
          children: [
            Column(
              children: [
                Expanded(
                  // Add a map view to the widget tree and set a controller.
                  child: ArcGISMapView(
                    controllerProvider: () => _mapViewController,
                    onMapViewReady: onMapViewReady,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    // Configure buttons to select the layer to display.
                    ElevatedButton(
                      onPressed: _fromUriActive ? null : createWmtsLayerFromUri,
                      child: const Text('From URI'),
                    ),
                    ElevatedButton(
                      onPressed:
                          _fromUriActive ? createWmtsLayerFromLayerInfo : null,
                      child: const Text('From LayerInfo'),
                    ),
                  ],
                ),
              ],
            ),
            // Display a progress indicator and prevent interaction until state is ready.
            Visibility(
              visible: !_ready,
              child: const SizedBox.expand(
                child: ColoredBox(
                  color: Colors.white30,
                  child: Center(child: CircularProgressIndicator()),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void onMapViewReady() async {
    // Initially display the map with the URI constructor.
    createWmtsLayerFromUri();
    // Set the ready state variable to true to enable the sample UI.
    setState(() => _ready = true);
  }

  void createWmtsLayerFromUri() {
    // Create a WMTS Layer using a URI and layer ID.
    final wmtsLayerFromUri = WmtsLayer.withUri(
      wmtsServiceUri,
      layerId: 'WorldTimeZones',
    );
    // Create a map and add the layer to the map's operational layers.
    final map = ArcGISMap();
    map.operationalLayers.add(wmtsLayerFromUri);
    // Set the map to the map view controller.
    _mapViewController.arcGISMap = map;

    // Set flag indicating the constructor being used.
    setState(() => _fromUriActive = true);
  }

  void createWmtsLayerFromLayerInfo() async {
    // Set the ready state variable to false to disable the sample UI.
    setState(() => _ready = false);
    // Create a map.
    final map = ArcGISMap();
    // Create a WMTS Layer using a WMTSLayerInfo.
    // Create a WMTS service and load.
    final service = WmtsService.withUri(wmtsServiceUri);
    await service.load();
    // Once loaded get the layer infos from the service info and create a WMTS layer from the first layer.
    if (service.serviceInfo != null &&
        service.serviceInfo!.layerInfos.isNotEmpty) {
      final layerInfos = service.serviceInfo!.layerInfos;
      final wmtsFromLayerInfo = WmtsLayer.withLayerInfo(layerInfos.first);
      // Create a basemap using the WMTS layer and set to the map.
      final basemap = Basemap.withBaseLayer(wmtsFromLayerInfo);
      map.basemap = basemap;
    }
    // Set the map to the map view controller.
    _mapViewController.arcGISMap = map;
    // Set the ready state variable to true to enable the sample UI.
    setState(() {
      _fromUriActive = false;
      _ready = true;
    });
  }
}

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