View on GitHub

Show a callout with formatted content for a KML feature.

Image of identify KML features

Use case

A user may wish to select a KML feature to view relevant information about it.

How to use the sample

Tap a feature to identify it. Feature information will be displayed in a callout.

Note: the KML layer used in this sample contains a screen overlay. The screen overlay contains a legend and the logos for NOAA and the NWS. You can’t identify the screen overlay.

How it works

  1. Create a KmlDataset from the forecast URL and use it to create a KmlLayer.
  2. Add the layer to an ArcGISMap and load it.
  3. Configure an onTap handler on the ArcGISMapView.
  4. Call ArcGISMapViewController.identifyLayer with the KML layer, tapped screen point, and a tolerance.
  5. Get the first KmlPlacemark from the result and convert its balloonContent HTML to styled Flutter text.
  6. Convert the tapped screen point to a map location and display the formatted content with ArcGISMapViewController.callout.showAt.

Note: There are several types of KML features. This sample only identifies features of type KmlPlacemark.

Relevant API

  • GeoViewController.identifyLayer
  • IdentifyLayerResult
  • KmlLayer
  • KmlPlacemark
  • KmlPlacemark.balloonContent

About the data

This sample shows a forecast for significant weather within the U.S. Regions of severe thunderstorms, flooding, snowfall, and freezing rain are shown. Tap the features to see details.

Additional information

KML features can have rich HTML content, including images.

Tags

Keyhole, KML, KMZ, NOAA, NWS, OGC, weather

Sample code

identify_kml_features.dart
// Copyright 2026 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/common/common.dart';
import 'package:material_ui/material_ui.dart';
import 'package:simple_html_css/simple_html_css.dart';
class IdentifyKmlFeatures extends StatefulWidget {
const IdentifyKmlFeatures({super.key});
@override
State<IdentifyKmlFeatures> createState() => _IdentifyKmlFeaturesState();
}
class _IdentifyKmlFeaturesState extends State<IdentifyKmlFeatures>
with SampleStateSupport {
// Create a controller for the map view.
final _mapViewController = ArcGISMapView.createController();
// Create a KML layer from the online weather forecast dataset.
final _forecastLayer = KmlLayer(
KmlDataset(
Uri.parse(
'https://www.arcgis.com/sharing/rest/content/items/f5e0e5cd088846a5b97b7ed66a8bad5c/data',
),
),
);
// A flag for when the map view is ready and controls can be used.
var _ready = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
left: false,
right: false,
child: Stack(
children: [
// Add a map view to the widget tree and set a controller.
ArcGISMapView(
controllerProvider: () => _mapViewController,
onMapViewReady: _onMapViewReady,
onTap: _onTap,
),
// Prompt the user to identify a KML feature.
const MapBanner(text: 'Tap a weather feature to view its details.'),
// Display a progress indicator and prevent interaction until the layer is ready.
LoadingIndicator(visible: !_ready),
],
),
),
);
}
Future<void> _onMapViewReady() async {
// Create a map with a dark gray basemap style and add the KML layer.
final map = ArcGISMap.withBasemapStyle(.arcGISDarkGrayBase);
map.operationalLayers.add(_forecastLayer);
_mapViewController.arcGISMap = map;
try {
// Load the KML layer before enabling identify operations.
await _forecastLayer.load();
// Zoom to the full extent of the weather forecast.
final fullExtent = _forecastLayer.fullExtent;
if (fullExtent != null) {
await _mapViewController.setViewpointGeometry(
fullExtent,
paddingInDiPs: 25,
);
}
} on Exception catch (exception) {
// Report a KML layer loading failure to the user.
showExceptionDialog('Failed to load KML layer', exception);
} finally {
// Hide the loading indicator after the layer finishes loading.
setState(() => _ready = true);
}
}
Future<void> _onTap(Offset screenPoint) async {
if (!_ready) return;
// Dismiss any callout from a previous identify operation.
_mapViewController.callout.dismiss(animated: false);
try {
// Identify the topmost geo element near the tapped screen point.
final identifyResult = await _mapViewController.identifyLayer(
_forecastLayer,
screenPoint: screenPoint,
tolerance: 22,
);
if (!mounted || identifyResult.error != null) return;
// Get the first identified geo element that is a KML placemark.
final placemark = identifyResult.geoElements
.whereType<KmlPlacemark>()
.firstOrNull;
if (placemark == null) return;
// Convert the tapped screen point to a map location for the callout anchor.
final tapLocation = _mapViewController.screenToLocation(
screen: screenPoint,
);
if (tapLocation == null) return;
// Supply fallback text when the placemark omits its description.
if (placemark.description.isEmpty) {
placemark.description = 'Weather condition';
}
// Show the placemark's formatted balloon content in a callout.
_mapViewController.callout.showAt(
tapLocation,
animated: false,
style: ThemedCalloutStyle.themed(context),
contentBuilder: (context, _) =>
_buildBalloonContent(context, placemark.balloonContent),
);
} on Exception catch (exception) {
// Report an identify failure to the user.
showExceptionDialog('Failed to identify KML feature', exception);
}
}
Widget _buildBalloonContent(BuildContext context, String htmlContent) {
// Normalize malformed NOAA HTML for simple_html_css's XML-based parser.
final normalizedHtml = htmlContent
.replaceAll(
RegExp(
r'<head\b[^>]*>.*?</head>',
caseSensitive: false,
dotAll: true,
),
'',
)
.replaceAllMapped(
RegExp('style="([^"]*);><td', caseSensitive: false),
(match) => 'style="${match.group(1)};"><td',
)
.replaceAll('&ge;', '&#8805;');
// Create theme-aware styles for the KML balloon's HTML content.
final colorScheme = Theme.of(context).colorScheme;
final defaultTextStyle = DefaultTextStyle.of(context).style
.copyWith(color: colorScheme.onSurface);
final overrideStyle = {
for (final tag in const ['body', 'p', 'div', 'span', 'table', 'tr', 'td'])
tag: TextStyle(color: colorScheme.onSurface),
'a': TextStyle(
color: colorScheme.primary,
decoration: TextDecoration.underline,
),
};
// Convert the balloon HTML to styled Flutter text.
return ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 240),
child: SingleChildScrollView(
child: RichText(
text: HTML.toTextSpan(
context,
normalizedHtml,
defaultTextStyle: defaultTextStyle,
overrideStyle: overrideStyle,
),
),
),
);
}
}