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
Create a KmlLayer from the KmlDataset which represents the KML file.
Add the KML layer to the SceneView's operational layers.
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.
When a node is selected, use the node's Extent to determine a viewpoint and set the SceneView object's viewpoint to it.
Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
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.javaMainActivity.java
/*
* 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;
publicclassKmlNodeAdapterextendsRecyclerView.Adapter<KmlNodeAdapter.KmlNodeViewHolder> {
privatefinal List<String> mNodeNames;
privatefinal List<BitmapDrawable> mKmlUxIcons;
privatefinal OnItemClickListener mOnItemClickListener;
staticclassKmlNodeViewHolderextendsRecyclerView.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);
}
}
publicinterfaceOnItemClickListener{
voidonItemClick(int position);
}
publicKmlNodeAdapter(List<String> nodeNames, List<BitmapDrawable> kmlUxIcons, OnItemClickListener onItemClickListener){
mNodeNames = nodeNames;
mKmlUxIcons = kmlUxIcons;
mOnItemClickListener = onItemClickListener;
}
@NonNull@Overridepublic 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;
}
@OverridepublicvoidonBindViewHolder(@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));
}
@OverridepublicintgetItemCount(){
return mNodeNames.size();
}
}