Keyhole Markup Language (KML) is an XML notation popularized by Google Earth and used to display geographic data for maps and scenes. KML is an extension of XML and uses a tag-based structure with nested elements and attributes. The KML file specifies a set of features (place marks, images, polygons, 3D models, textual descriptions, etc.) that can be displayed on maps in geospatial software implementing the KML encoding, include ArcGIS.

Along with geographic positioning information, KML files provide supporting content (including images and 3D models) and are often distributed in a zipped KMZ archive file. The contents of a KMZ file are a single root KML document (notionally “doc.kml”) and optionally any overlays, images, icons, and COLLADA 3D models referenced in the KML, including network-linked KML files.

KML specification version 2.2 as defined by the Open GIS Consortium (OGC) is supported. Use the free Esri product ArcGIS Earth to read and write KML and KMZ files. See this Google KML Tutorial to learn more about the KML file structure, commonly used tags, and so on.

Display a KML/KMZ file

KML content is loaded using a KmlDataset. The KmlDataset 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.

// Create a KML dataset from the URL of a KMZ file.
final kmlDataset = KmlDataset(
Uri.parse(
'https://www.arcgis.com/sharing/rest/content/items/'
'f10b1d37fdd645c9bc9b189fb546307c/data',
),
);
// Create a KML layer from the dataset.
final layer = KmlLayer(kmlDataset);
// Load the KML layer to load the associated dataset.
await layer.load();

    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 five 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.

    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 KmlDataset, which exposes the KML feature tree with the rootNodes property. This provides a a list of KmlNode objects.

    The non-recursive part of turning off a screen overlay is toggling its visibility after it is found. Because you are working with a generic KmlNode, you first need to determine whether each iterated node is a KmlScreenOverlay. After the screen overlay is found, toggle its visibility.

    void exploreKml(List<KmlNode> nodes) {
    for (final node in nodes) {
    // Hide each KML screen overlay found in the KML content tree.
    if (node is KmlScreenOverlay) node.isVisible = false;
    }
    }

      You should then look for screen overlay nodes that may exist deeper in the hierarchy, however. Recursively call the exploreKml function (shown next) on any nodes that have children.

      void exploreKml(List<KmlNode> nodes) {
      for (final node in nodes) {
      // Hide each KML screen overlay found in the KML content tree.
      if (node is KmlScreenOverlay) node.isVisible = false;
      // Recursively search the child nodes of containers
      // and network links.
      exploreKml(childNodes(node));
      }
      }
      // Return child nodes only for KML node types that can contain nested content.
      List<KmlNode> childNodes(KmlNode node) {
      // KML containers group KML nodes in folders or documents.
      if (node is KmlContainer) return node.childNodes;
      // KML network links can load additional nodes from another source.
      if (node is KmlNetworkLink) return node.childNodes;
      // Other KML node types do not have child nodes.
      return [];
      }
        After coding your recursive method for toggling screen overlay visibility, invoke it initially by passing in the KmlDataset.rootNodes property. You can invoke your method directly after loading the map or when handling user actions (such as a button press).

        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:

        Layer list showing a KML tree with US Civil War > Major Battles and placemarks like Fort Sumter and First Bull Run.

        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 HTML annotation via KmlNode ballonContent and then display that using a web view.

        Viewpoints in KML

        The OGC KML specification 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

        The KmlViewpoint class can represent both types of viewpoints. Custom code is required to convert from a KML viewpoint to an ArcGIS 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.

        Viewpoint? viewpointFromKml(KmlNode node) {
        // Get the KML viewpoint associated with
        // the node, if it has one.
        final kmlViewpoint = node.viewpoint;
        if (kmlViewpoint == null) return null;
        // Create an ArcGIS camera that corresponds
        // to the KML viewpoint type.
        final Camera camera;
        switch (kmlViewpoint.type) {
        case KmlViewpointType.lookAt:
        // Position the camera relative to the KML
        // point of interest.
        camera = Camera.withLookAtPoint(
        lookAtPoint: kmlViewpoint.location,
        distance: kmlViewpoint.range,
        heading: kmlViewpoint.heading,
        pitch: kmlViewpoint.pitch,
        roll: kmlViewpoint.roll,
        );
        case KmlViewpointType.camera:
        // Position the camera at the location specified
        // in the KML viewpoint.
        camera = Camera.withLocation(
        location: kmlViewpoint.location,
        heading: kmlViewpoint.heading,
        pitch: kmlViewpoint.pitch,
        roll: kmlViewpoint.roll,
        );
        case KmlViewpointType.unknown:
        // A viewpoint cannot be created for an unsupported
        // KML viewpoint type.
        return null;
        }
        // Combine the KML location and converted camera
        // into an ArcGIS viewpoint.
        return Viewpoint.withPointScaleCamera(
        center: kmlViewpoint.location,
        scale: kmlViewpoint.range,
        camera: camera,
        );
        }

          Play KML tours§

          Playing tours contained in KML files is supported. A KML tour defines a map exploration experience that can contain: narration and music, animated movement between viewpoints, and dynamic animation of KML feature properties.

          To play a KML tour, create a KmlTourController and set its tour property to the tour you want to play. Then use the play, pause, and restart methods on KmlTourController 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, you can check to the tourStatus property on KmlTour.

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

          See Touring in KML on the Google developers site to learn more about KML Tours.

          § This capability has not yet been implemented in ArcGIS Maps SDK for Flutter, but will be added in a future release. See this page for more details.

          Samples