Select to view content in your preferred language

Load, edit and save kmz file

11-15-2022 03:21 AM
New Contributor

Hello all,

I've been working on an WinUI app in which I can load and display a kmz file as an operational layer on a mapview. But, I want also to edit and save the kmz file with the added information.

To load the information, I use the following:

kmlLayer = new KmlLayer(new Uri(@"C:\Users\markv\Downloads\Trektochtlocaties.kmz"));

I am also able to add a placemark using:

                    SketchCreationMode creationMode;
                    creationMode = SketchCreationMode.Point;
                    Geometry geometry = await MapView.SketchEditor.StartAsync(creationMode, true);
                    Geometry projectedGeometry = GeometryEngine.Project(geometry, SpatialReferences.Wgs84);
                    KmlGeometry kmlGeometry = new KmlGeometry(projectedGeometry, KmlAltitudeMode.ClampToGround);
                    currentPlacemark = new KmlPlacemark(kmlGeometry);
                    currentPlacemark.Style = new KmlStyle();
                    currentPlacemark.Style.IconStyle = new KmlIconStyle(new KmlIcon(new Uri("")), 1.0);
                catch { }

and then save a new file using the following code:

                    //Remove old file
                    try { System.IO.File.Delete(@"C:\Users\markv\Downloads\Trektochtlocaties_old.kmz"); } catch { }

                    //Rename and delete current file
                    System.IO.File.Copy(@"C:\Users\markv\Downloads\Trektochtlocaties.kmz", @"C:\Users\markv\Downloads\Trektochtlocaties_old.kmz");

                    //Save new file
                    await kmlDocument.SaveAsAsync(@"C:\Users\markv\Downloads\Trektochtlocaties.kmz");
                catch { }

 So, I can load the existing file, and seperate from that add placemarks to a new kmlDocument and save that to a file. But, this causes the existing file to be overwritten with the new file, so the existing information disappears.


How can I combine these codes in order to load a file, edit that file and save all the information? Thanks!

0 Kudos
0 Replies