ArcGIS Runtime SDK for Java

Display KML content

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

KmlDataset fileDataSource = new KmlDataset(filePath);
KmlLayer displayLayer = new KmlLayer(fileDataSource);

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.

Exploring 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. KmlDataset exposes the KML feature tree using thegetRootNodes method, which returns a list of KmlNodes. Start by coding a method that accepts a list of KmlNode.

private void exploreKml(List<KmlNode> nodes) {
  for (KmlNode node : nodes) {
      // Do work here...
  }
}

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 KmlNode, you first need to determine whether each iterated node is a screen overlay. After the screen overlay is found, toggle its visibility.

private void exploreKml(List<KmlNode> nodes) {
  for (KmlNode node : nodes){
    if (node instanceof KmlScreenOverlay) {
      node.setVisible(false);
    }
    exploreKml(childNodes(node));
  }
}

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

private void exploreKml(List<KmlNode> nodes) {
  for (KmlNode node : nodes){
    if (node instanceof KmlScreenOverlay) {
      node.setVisible(false);
    }
    exploreKml(childNodes(node));
  }
}

// returns all the child nodes of this node
private List<KmlNode> childNodes(KmlNode node) {
  List<KmlNode> children = new ArrayList<>();
  if (node instanceof KmlContainer) {
    children.addAll(((KmlContainer) node).getChildNodes());
  }
  if (node instanceof KmlNetworkLink) {
    children.addAll(((KmlNetworkLink) node).getChildNodes());
  }
  return children;
}

After coding your recursive method for toggling screen overlay visibility, invoke it initially by passing in a call to KMLDataSet's getRootNodes method. You can invoke your method directly after loading the map or when handling user actions (like 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:

Screenshot of ArcGIS Earth's table of contents, with a KML file open.

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 KmlNode.BalloonContent, 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

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