Identify layer features

View on GitHub

Identify features in all layers in a map.

Image of identify layer features

Use case

This sample allows users to tap on a map, returning features at that location across multiple layers. Because some layer types have sublayers, the sample recursively counts results for sublayers within each layer.

How to use the sample

Tap to identify features. A banner will list layers with features under the tapped location, as well as a layer count.

How it works

  1. The tapped position is passed to ArcGISMapViewController.identifyLayers.
  2. For each IdentifyLayerResult in the results, features are counted.

Note: there is one identify result per layer with matching features; if the feature count is 0, that means a sublayer contains the matching features.

Relevant API

  • ArcGISMapViewController.identifyLayers
  • IdentifyLayerResult
  • IdentifyLayerResult.layerContent.name
  • IdentifyLayerResult.sublayerResults

Additional information

The GeoViewController supports two methods of identify: identifyLayer(), which identifies features within a specific layer, and identifyLayers(), which identifies features for all layers in the current view.

Tags

identify, recursion, recursive, sublayers

Sample Code

identify_layer_features.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// 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:flutter/material.dart';

import '../../utils/sample_state_support.dart';

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

  @override
  State<IdentifyLayerFeatures> createState() => _IdentifyLayerFeaturesState();
}

class _IdentifyLayerFeaturesState extends State<IdentifyLayerFeatures>
    with SampleStateSupport {
  // Create a controller for the map view.
  final _mapViewController = ArcGISMapView.createController();
  // A flag for when the map view is ready and controls can be used.
  var _ready = false;
  // The message to display in the result banner.
  var _message = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          // Add a map view to the widget tree and set a controller.
          ArcGISMapView(
            controllerProvider: () => _mapViewController,
            onMapViewReady: onMapViewReady,
            onTap: onTap,
          ),
          // Add a banner to show the results of the identify operation.
          SafeArea(
            child: IgnorePointer(
              child: Visibility(
                visible: _message.isNotEmpty,
                child: Container(
                  padding: const EdgeInsets.all(10),
                  color: Colors.black.withOpacity(0.7),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        _message,
                        textAlign: TextAlign.center,
                        style: const TextStyle(color: Colors.white),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
          // Display a progress indicator and prevent interaction until state is ready.
          Visibility(
            visible: !_ready,
            child: SizedBox.expand(
              child: Container(
                color: Colors.white30,
                child: const Center(child: CircularProgressIndicator()),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void onMapViewReady() async {
    // Create a feature layer of damaged property data.
    final serviceFeatureTable = ServiceFeatureTable.withUri(
      Uri.parse(
        'https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0',
      ),
    );
    final featureLayer = FeatureLayer.withFeatureTable(serviceFeatureTable);

    // Create a layer with world cities data.
    final mapImageLayer = ArcGISMapImageLayer.withUri(
      Uri.parse(
        'https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer',
      ),
    );
    await mapImageLayer.load();
    // Hide continent and world layers.
    mapImageLayer.subLayerContents[1].isVisible = false;
    mapImageLayer.subLayerContents[2].isVisible = false;

    // Create a map with a basemap style and an initial viewpoint.
    final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
    map.initialViewpoint = Viewpoint.fromCenter(
      ArcGISPoint(
        x: -10977012.785807,
        y: 4514257.550369,
        spatialReference: SpatialReference.webMercator,
      ),
      scale: 68015210,
    );

    // Add the layers to the map.
    map.operationalLayers.addAll([featureLayer, mapImageLayer]);

    // Add the map to the map view.
    _mapViewController.arcGISMap = map;

    // Set the ready state variable to true to enable the sample UI.
    setState(() {
      _message = 'Tap on the map to identify layer features.';
      _ready = true;
    });
  }

  void onTap(Offset localPosition) async {
    // Identify features at the tapped location.
    final identifyLayerResults = await _mapViewController.identifyLayers(
      screenPoint: localPosition,
      tolerance: 12.0,
      maximumResultsPerLayer: 10,
    );

    // Count the number of identified features.
    var identifyTotal = 0;
    final layerCounts = <String>[];
    for (final result in identifyLayerResults) {
      final layerTotal = result.totalCount;
      identifyTotal += layerTotal;
      layerCounts.add('${result.layerContent.name}: $layerTotal');
    }

    // Display the results in the banner.
    setState(() {
      _message = identifyTotal == 0
          ? 'No features identified.'
          : layerCounts.join('\n');
    });
  }
}

extension on IdentifyLayerResult {
  // The total count of features, recursively including all sublayers.
  int get totalCount {
    return sublayerResults.fold(
      geoElements.length,
      (previous, element) => previous + element.totalCount,
    );
  }
}

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