List KML contents

View on GitHubSample viewer app

List the contents of a KML file.

Image of list KML contents

Use case

KML files can contain a hierarchy of features, including network links to other KML content. A user may wish to traverse through the contents of KML nodes to know what data is contained within each node and, recursively, their children.

How to use the sample

The contents of the KML file are shown in a tree. Select a node to zoom to that node. Not all nodes can be zoomed to (e.g. screen overlays).

How it works

  1. Create a KmlLayer from the KmlDataset which represents the KML file.
  2. Add the KML layer to the SceneView's operational layers.
  3. Recursively explore each node of the KmlDataSet starting from the root nodes.
  • Each node is enabled for display at this step. KML files may include nodes that are turned off by default.
  1. When a node is selected, use the node's Extent to determine a viewpoint and set the SceneView object's viewpoint to it.

Relevant API

  • KmlContainer
  • KmlDataset
  • KmlDocument
  • KmlFolder
  • KmlGroundOverlay
  • KmlLayer
  • KmlNetworkLink
  • KmlNode
  • KmlPlacemark
  • KmlScreenOverlay

Offline Data

  1. Download the data from ArcGIS Online.
  2. Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
  3. Push the data into the scoped storage of the sample app:

adb push esri_test_data.kmz /Android/data/com.esri.arcgisruntime.sample.listkmlcontents/files/esri_test_data.kmz

Tags

Keyhole, KML, KMZ, layers, OGC

Sample Code

KmlNodeAdapter.javaKmlNodeAdapter.javaMainActivity.java
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
/*
 *  Copyright 2019 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
 *
 *     http://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.
 */

package com.esri.arcgisruntime.sample.listkmlcontents;

import java.util.List;

import android.graphics.drawable.BitmapDrawable;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class KmlNodeAdapter extends RecyclerView.Adapter<KmlNodeAdapter.KmlNodeViewHolder> {
  private final List<String> mNodeNames;
  private final List<BitmapDrawable> mKmlUxIcons;
  private final OnItemClickListener mOnItemClickListener;

  static class KmlNodeViewHolder extends RecyclerView.ViewHolder {
    final TextView textView;
    final ImageView imageView;
    KmlNodeViewHolder(View itemView) {
      super(itemView);
      itemView.setClickable(true);
      textView = itemView.findViewById(R.id.nodeRowTextView);
      imageView = itemView.findViewById(R.id.nodeRowImageView);
    }
  }

  public interface OnItemClickListener {
    void onItemClick(int position);
  }

  public KmlNodeAdapter(List<String> nodeNames, List<BitmapDrawable> kmlUxIcons, OnItemClickListener onItemClickListener) {
    mNodeNames = nodeNames;
    mKmlUxIcons = kmlUxIcons;
    mOnItemClickListener = onItemClickListener;
  }

  @NonNull @Override
  public KmlNodeAdapter.KmlNodeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.node_row, parent, false);
    KmlNodeViewHolder kmlNodeViewHolder = new KmlNodeViewHolder(view);
    kmlNodeViewHolder.setIsRecyclable(false);
    return kmlNodeViewHolder;
  }

  @Override
  public void onBindViewHolder(@NonNull KmlNodeViewHolder holder, int position) {
    holder.textView.setText(mNodeNames.get(position));
    if (position < mKmlUxIcons.size() && mKmlUxIcons.get(position) != null) {
      holder.imageView.setImageDrawable(mKmlUxIcons.get(position));
    }
    holder.itemView.setOnClickListener(v -> mOnItemClickListener.onItemClick(position));
  }

  @Override
  public int getItemCount() {
    return mNodeNames.size();
  }
}

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