Create and save KML file

View inWPFUWPFormsiOSAndroid
View on GitHub

Construct a KML document and save it as a KMZ file.

Image of create and save KML file

Use case

If you need to create and save data on the fly, you can use KML to create points, lines, and polygons by sketching on the map, customizing the style, and serializing them as KML nodes in a KML Document. Once complete, you can share the KML data with others that are using a KML reading application, such as ArcGIS Earth.

How to use the sample

Tap on one of the buttons in the middle row to start adding a geometry. Tap on the map view to place vertices. Tap the "Complete Sketch" button to add the geometry to the KML document as a new KML placemark. Use the style interface to edit the style of the placemark. If you do not wish to set a style, tap the "Don't Apply Style" button. When you are finished adding KML nodes, tap on the "Save KMZ file" button to save the active KML document as a .kmz file on your system. Use the "Reset" button to clear the current KML document and start a new one.

How it works

  1. Create a KmlDocument
  2. Create a KmlDataset using the KmlDocument.
  3. Create a KmlLayer using the KmlDataset and add it to Map.OperationalLayers.
  4. Create Geometry using SketchEditor.
  5. Project that Geometry to WGS84 using GeometryEngine.Project.
  6. Create a KmlGeometry object using that projected Geometry.
  7. Create a KmlPlacemark using the KmlGeometry.
  8. Add the KmlPlacemark to the KmlDocument.
  9. Set the KmlStyle for the KmlPlacemark.
  10. When finished with adding KmlPlacemark nodes to the KmlDocument, save the KmlDocument to a file using the SaveAsAsync method.

Relevant API

  • GeometryEngine.Project
  • KmlDataset
  • KmlDocument
  • KmlGeometry
  • KmlLayer
  • KmlNode.SaveAsASync
  • KmlPlacemark
  • KmlStyle
  • SketchEditor

Tags

Keyhole, KML, KMZ, OGC

Sample Code

CreateAndSaveKmlFile.axmlCreateAndSaveKmlFile.cs
                                                                                                  
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_main"
        tools:context=".MainActivity">
    <Esri.ArcGISRuntime.UI.Controls.MapView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/MapView"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="22dp"
            android:background="@android:color/background_dark"
            android:alpha="0.5"/>
        <TextView
            android:id="@+id/statusLabel"
            android:layout_width="match_parent"
            android:layout_height="22dp"
            android:layout_marginLeft="5dp"
            android:text="Press the 'Add Item' button to start"
            android:textStyle="bold"
            android:textColor="@android:color/white"/>
    </RelativeLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout"
        app:layout_constraintTop_toBottomOf="@+id/MapView"
        app:layout_constraintBottom_toBottomOf="parent"
        android:showDividers="middle"
        android:divider="@android:drawable/divider_horizontal_dark">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Add item"
            android:id="@+id/addButton" />
        <Space
            android:layout_width="0dp"
            android:layout_height="wrap_content"
        android:layout_weight="1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/saveButton"
            android:text="Save"
        android:layout_weight="1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/resetButton"
            android:text="Reset"
        android:layout_weight="1"/>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/completeButton"
        android:text="Complete"
        app:layout_constraintTop_toBottomOf="@+id/MapView"
        app:layout_constraintBottom_toBottomOf="parent"
        android:visibility="invisible"/>
    <LinearLayout
        android:id="@+id/PickerUI"
        android:visibility="invisible"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/styleText"
            android:text="testing text"
            android:layout_weight="0.5"/>
        <ListView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/listView"
            android:layout_weight="7"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/noStyleButton"
            android:text="No Style"
        android:layout_weight="0.75"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

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