Display KML content

In this topic

Keyhole Markup Language (KML) is a geographic data format popularized by Google Earth. KML files can be distributed with supporting content, including images and 3D models, in a KMZ archive.

Display a KML/KMZ file

KML content is loaded using a AGSKMLDataset. The AGSKMLDataset constructor takes a URL, which can point to a local file or a network location. Loading the layer will cause the associated dataset to load. KML layers can also be loaded from portal items, which can specify a link to a file or the KML/KMZ file itself.

Note:

See the licensing guide to learn more about the licensing requirements for KML files loaded over the network and from disk.

Many KML files are wrappers that point to resources over the network. For example, a weather map might consist of a single network link that points to the latest forecast, to be retrieved every 5 minutes. As a result, loading the layer does not necessarily guarantee that the content has loaded – linked content can fail to load without affecting the load status of the layer.

Note:

Many KML files, even those delivered over secure HTTPS connections, point to resources via insecure HTTP links. These resources may fail to load as a consequence of App Transport Security on iOS. Add appropriate ATS exceptions as needed for KML content. See Apple’s developer documentation for more information about ATS exceptions.

Explore the KML content tree

KML layers contain content in a hierarchy. You may need to programmatically explore this hierarchy to interact with KML content. For example, to turn off a screen overlay, you would need to first find it in the tree, then change its visibility. This code will follow a recursive pattern, using a function that calls itself for each node in the tree. The KML tree should be explored starting with AGSKMLDataset. AGSKMLDataset exposes the KML feature tree with the rootNodes property. This provides a collection of AGSKMLNodes. Start with a function that accepts an enumeration of AGSKMLNodes.

The non-recursive part of turning off a screen overlay is toggling its visibility once it's found. Because you're working with a generic AGSKMLNode, you first need to determine whether each iterated node is a screen overlay, AGSKMLScreenOverlay. After the screen overlay is found, toggle its visibility.

The tree-exploration pattern is useful for other tasks involving KML. For example, ArcGIS Earth uses a recursive pattern to build a table of contents:

Identify and popups in KML

In the ArcGIS information model, a popup is defined with a set of fields that describe an object, including how that information is formatted. Unlike ArcGIS feature services, KML files don't define a standard schema for object attributes. Instead, KML files may provide a rich HTML annotation for each object, which can be presented in place of a popup. You can access the the HTML annotation via ballonContent, then display that using a webview.

Viewpoints in KML

KML has two primary ways for defining viewpoints:

LookAt - defines a camera relative to the position of a KML feature

Camera - defines the position of the camera explicitly

AGSKmlViewpoint can represent both types of viewpoints. Custom code is required to convert from a KML viewpoint to an ArcGIS Runtime viewpoint, which can be used for navigating a scene. See Google's reference documentation for details, including diagrams. Earth browsing apps should respect LookAt viewpoints specified in KML content.

To play a KML tour, create a KML tour controller and set its Tour property to the tour you want to play. Then use the Play, Pause, and Restart methods on AGSKmlTourController to work with the tour. If the tour contains audio, that audio will play automatically with the tour. To monitor the status of the tour, refer to the AGSKmlTour.TourStatus property.

The following example shows a function that can search KML nodes for a tour and assign the first one it finds to a AGSKmlTourController.