Drag and Drop

Download Samples Repository

Description

This sample demonstrates how you can add KML or KMZ file from your machine to the map using Drag/Drop.

"Desktop" Available for Desktop

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.DragDropKMLFile"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <esri:MapView x:Name="MyMapView" WrapAround="True" Drop="MyMapView_Drop" AllowDrop="True">
            <esri:Map>
                <esri:ArcGISTiledMapServiceLayer
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" />
            </esri:Map>
        </esri:MapView>
        <Border Background="White" BorderBrush="Black" BorderThickness="1"
				HorizontalAlignment="Right" VerticalAlignment="Top"
				Margin="30" Padding="20"
				Width="200">
            <Border.Effect>
                <DropShadowEffect/>
            </Border.Effect>
            <StackPanel>
                <TextBlock Text="Drag and drop any KML or KMZ file onto the map." TextWrapping="Wrap" />
                <Button Content="Reset" Click="ResetMapButton_Click" Margin="15"/>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
using Esri.ArcGISRuntime.Layers;


namespace ArcGISRuntime.Samples.Desktop
{
    /// <summary>
    /// This sample demonstrates how you can add KML or KMZ file from your machine to the map using Drag/Drop. 
    /// </summary>
    /// <title>Drag and Drop</title>
    /// <category>Layers</category>
    /// <subcategory>Kml Layers</subcategory>
    public partial class DragDropKMLFile : UserControl
    {
        public DragDropKMLFile()
        {
            InitializeComponent();
        }

        private async void MyMapView_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                KmlLayer kmlLayer = new KmlLayer(new Uri(files[0]));
                await kmlLayer.InitializeAsync();

                //Add the kml layer
                MyMapView.Map.Layers.Add(kmlLayer);

                //Zoom to the kml layer if available
                if (kmlLayer.RootFeature.Viewpoint != null)
                    await MyMapView.SetViewAsync(kmlLayer.RootFeature.Viewpoint);
            }
        }

        private void ResetMapButton_Click(object sender, RoutedEventArgs e)
        {
            MyMapView.Map.Layers.Clear();
            MyMapView.Map.Layers.Add(new ArcGISTiledMapServiceLayer(new Uri("http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer")));
        }
    }
}
Feedback on this topic?