Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Display KML

This code sample is available for these platforms:
View Sample on GitHub

Load and display KML from a URL, a local file, or a portal item.

How to use the sample

Use the UI to select a source. A KML file from that source will be loaded and displayed in the map.

Relevant API

  • KmlLayer
  • KmlLayer(Uri)
  • KmlLayer(PortalItem)

Offline data

This sample uses US state capitals KML file. It is downloaded from ArcGIS Online automatically.

About the maps

This sample displays three different KML files:

  • From URL - this is a map of the significant weather outlook produced by NOAA/NWS. It uses KML network links to always show the latest data.
  • From local file - this is a map of U.S. state capitals. It doesn't define an icon, so the default pushpin is used for the points.
  • From portal item - this is a map of U.S. states.


KML, OGC, KMZ, Keyhole

Sample Code

<?xml version="1.0" encoding="utf-8"?>

<ContentPage xmlns=""
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        <Label Grid.Row="0" Text="Choose a KML data source." />
        <Picker Grid.Row="1" x:Name="LayerPicker" />
        <esriUI:SceneView Grid.Row="2" x:Name="MySceneView" />
// Copyright 2018 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:
// 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.

using System;
using ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using Xamarin.Forms;

namespace ArcGISRuntimeXamarin.Samples.DisplayKml
        "Display KML",
        "Display a KML file from URL, a local file, or a portal item.",
    public partial class DisplayKml : ContentPage
        private readonly Envelope _usEnvelope = new Envelope(-144.619561355187, 18.0328662832097, -66.0903762761083, 67.6390975806745, SpatialReferences.Wgs84);
        private readonly string[] _sources = {"URL", "Local file", "Portal item"};

        public DisplayKml()

        private void Initialize()
            // Set up the basemap.
            MySceneView.Scene = new Scene(Basemap.CreateImageryWithLabels());

            // Update the UI.
            LayerPicker.IsEnabled = true;
            LayerPicker.ItemsSource = _sources;
            LayerPicker.SelectedIndexChanged += LayerPicker_SelectionChanged;
            LayerPicker.SelectedIndex = 0;

        private async void LayerPicker_SelectionChanged(object sender, EventArgs e)
            // Clear existing layers.

            // Get the name of the selected layer.
            string name = _sources[LayerPicker.SelectedIndex];

                // Create the layer using the chosen constructor.
                KmlLayer layer;
                switch (name)
                    case "URL":
                        layer = new KmlLayer(new Uri(""));
                    case "Local file":
                        string filePath = DataManager.GetDataFolder("324e4742820e46cfbe5029ff2c32cb1f", "US_State_Capitals.kml");
                        layer = new KmlLayer(new Uri(filePath));
                    case "Portal item":
                        ArcGISPortal portal = await ArcGISPortal.CreateAsync();
                        PortalItem item = await PortalItem.CreateAsync(portal, "9fe0b1bfdcd64c83bd77ea0452c76253");
                        layer = new KmlLayer(item);

                // Add the selected layer to the map.

                // Zoom to the extent of the United States.
                await MySceneView.SetViewpointAsync(new Viewpoint(_usEnvelope));
            catch (Exception ex)
                await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");