View on GitHub Sample viewer app

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 editing geometries on the map using the geometry editor, 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

Select the geometry type for editing with the first combo box. Change the style for the KML feature using the second combo box.

Click anywhere on the map to edit. Press ENTER to commit the geometry or ESCAPE to discard the geometry.

When finished, click the ‘Save KMZ file’ button to save the KML document to a KMZ file.

How it works

  1. Create a KmlDocument
  2. Create a KmlDataset using the KML document.
  3. Create a KmlLayer using the KML dataset and add it to the map as an operational layer.
  4. Create Geometry using GeometryEditor.
  5. Project that geometry to WGS84 using GeometryEngine.project.
  6. Create a KmlGeometry object using the projected geometry.
  7. Create a KmlPlacemark using the KML geometry.
  8. Add the KML placemark to the KML document.
  9. Set the KmlStyle for the KML placemark.
  10. Save the KML document to a file using kmlDocument.saveAsAsync(Path).

Relevant API

  • GeometryEditor
  • GeometryEngine.Project
  • KmlDataset
  • KmlDocument
  • KmlGeometry
  • KmlLayer
  • KmlPlacemark
  • KmlStyle

Tags

Keyhole, KML, KMZ, OGC

Sample Code

module-info.java module-info.java CreateAndSaveKMLFileController.java CreateAndSaveKMLFileSample.java ImageURLListCell.java
/*
* Copyright 2022 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.
*/
module com.esri.samples.create_and_save_kml_file {
// require ArcGIS Maps SDK for Java module
requires com.esri.arcgisruntime;
// handle SLF4J http://www.slf4j.org/codes.html#StaticLoggerBinder
requires org.slf4j.nop;
// require JavaFX modules that the application uses
requires javafx.graphics;
requires javafx.controls;
requires javafx.fxml;
// make all annotated @FXML objects reflectively accessible to the javafx.fxml module
opens com.esri.samples.create_and_save_kml_file to javafx.fxml;
exports com.esri.samples.create_and_save_kml_file;
}