Create and save KML file

View inQMLC++
View on GitHub
Sample viewer app

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

screenshot

Use case

If you need to create and save data on the fly, you can use KML to create points, lines, and polygons and then 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

Application opens to a view of the Southwestern United States. Click on the "Save KMZ file" button to save the active KML document as a .kmz file on your system.

How it works

  1. Create a Point, Polyline, and Polygon.
  2. Create a Graphic for each and add it to the GraphicsOverlay
  3. Create a KmlGeometry object using the Geometry from the point, line, and polygon.
  4. Create a KmlPlacemark for each kml geometry.
  5. Set a KmlStyle for each placemark.
  6. Add all three kml placemarks to the KmlDocument.
  7. Save the kml document to a file using the saveAs function.

Relevant API

  • KmlDocument
  • KmlGeometry
  • KmlNode::saveAs
  • KmlPlacemark
  • KmlStyle
  • KmlIconStyle
  • KmlLineStyle
  • KmlPolygonStyle
  • KmlIcon

Tags

Keyhole, KML, KMZ, OGC

Sample Code

CreateAndSaveKmlFile.qmlCreateAndSaveKmlFile.cppCreateAndSaveKmlFile.h
                                                                                 
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
// [WriteFile Name=CreateAndSaveKmlFile, Category=Layers]
// [Legal]
// 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.
// [Legal]

import QtQuick 2.6
import QtQuick.Controls 2.2
import Qt.labs.platform 1.1 as Dialogs
import Esri.Samples 1.0

Item {

    // add a mapView component
    MapView {
        id: view
        anchors.fill: parent
    }

    // Declare the C++ instance which creates the scene etc. and supply the view
    CreateAndSaveKmlFileSample {
        id: model
        mapView: view

        onKmlSaveCompleted: saveCompleteDialog.open();
    }

    Button {
        anchors{
            left: parent.left
            top: parent.top
            margins: 3
        }
        text: qsTr("Save kmz file")

        onClicked: {
            fileDialog.visible = true;
        }
    }

    BusyIndicator {
        anchors.centerIn: parent
        visible: model.busy
    }

    Dialogs.FileDialog {
        id: fileDialog
        defaultSuffix: "kmz"
        fileMode: Dialogs.FileDialog.SaveFile
        nameFilters: ["Kml files (*.kmz *.kml)"]
        onAccepted: {
            visible: false;
            model.saveKml(currentFile);
        }
        onRejected: {
            visible: false;
        }
    }

    Dialog {
        id: saveCompleteDialog
        anchors.centerIn: parent
        modal: true
        standardButtons: Dialog.Ok
        Text {
            id:textLabel
            anchors.centerIn: parent
            text: qsTr("Item saved.")
        }
    }
}

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