Snippets

Philip O'Gorman ArcGis KmlLayer with GroundOverlay

Updated by Philip O'Gorman

File KmlOverlay.cs Modified

  • Ignore whitespace
  • Hide word diff
+using Esri.ArcGISRuntime.Mapping;
+using SharpKml.Base;
 using SharpKml.Dom;
 using SharpKml.Dom.GX;
 using SharpKml.Engine;
 using System;
 using System.IO;
+using Xamarin.Forms;
 
 namespace FormsMapsShared.ArcGisRendering
-
-public class TractorRenderer
+{
+    public class TractorRenderer
     {
         Esri.ArcGISRuntime.Xamarin.Forms.MapView _mapView;
+        KmlLayer _tractorKml;
         string kmlPath = "/storage/emulated/0/SmartMaps/tractor.kml";
         public TractorRenderer(Esri.ArcGISRuntime.Xamarin.Forms.MapView mapView)
         {
             {
                 File.Delete(kmlPath);
             }
-  
+            if (implment.TractorCenter == null) { return; }
+
             var quad = new LatLonQuad();
             quad.Coordinates = new CoordinateCollection();
-            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC1.Latitude, implment.GraphicC1.Longitude));
-            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC2.Latitude, implment.GraphicC2.Longitude));
+            
             quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC3.Latitude, implment.GraphicC3.Longitude));
             quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC4.Latitude, implment.GraphicC4.Longitude));
-
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC1.Latitude, implment.GraphicC1.Longitude));
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC2.Latitude, implment.GraphicC2.Longitude));
             var folder = new Folder();
             folder.Id = "f0";
             folder.Name = "Folder 0";
             }
 
             Uri sourceUri = new Uri(kmlPath);
-            var tractorKml = new KmlLayer(sourceUri);
-            tractorKml.LoadAsync();
-            _mapView.Map.Basemap.BaseLayers.Clear();
-            _mapView.Map.Basemap.BaseLayers.Add(tractorKml);
+            var prev = _tractorKml;
+            _tractorKml = new KmlLayer(sourceUri);
+            _tractorKml.LoadAsync();
+
+            // Add created overlay to the MapView
+            if (prev != null) { _mapView.Map.Basemap.BaseLayers.Remove(prev); }
+            _mapView.Map.Basemap.BaseLayers.Add(_tractorKml);
+            
         }
-    }
+
+        
+    }
+}
Updated by Philip O'Gorman

File kml output.xml Added

  • Ignore whitespace
  • Hide word diff
+<?xml version="1.0" encoding="utf-8"?>
+<kml xmlns="http://www.opengis.net/kml/2.2">
+  <Folder id="f0">
+    <name>Folder 0</name>
+    <GroundOverlay>
+      <Icon>
+        <href>tractor.png</href>
+      </Icon>
+      <LatLonQuad xmlns="http://www.google.com/kml/ext/2.2">
+        <coordinates xmlns="http://www.opengis.net/kml/2.2">
+          -93.404613486639704,41.237868244845878
+          -93.404685721065661,41.238025092434761
+          -93.404517075653644,41.238017561915193
+          -93.404527090083818,41.23789074434336
+        </coordinates>
+      </LatLonQuad>
+    </GroundOverlay>
+  </Folder>
+</kml>
Updated by Philip O'Gorman

File KmlOverlay Deleted

  • Ignore whitespace
  • Hide word diff
-using SharpKml.Dom;
-using SharpKml.Dom.GX;
-using SharpKml.Engine;
-using System;
-using System.IO;
-
-namespace FormsMapsShared.ArcGisRendering
-
-public class TractorRenderer
-    {
-        Esri.ArcGISRuntime.Xamarin.Forms.MapView _mapView;
-        string kmlPath = "/storage/emulated/0/SmartMaps/tractor.kml";
-        public TractorRenderer(Esri.ArcGISRuntime.Xamarin.Forms.MapView mapView)
-        {
-            _mapView = mapView;      
-        }
-
-
-        public void UpdateTractorPosition(IImplement implment)
-        {
-            //delete the file from the last update
-            if(File.Exists(kmlPath))
-            {
-                File.Delete(kmlPath);
-            }
-  
-            var quad = new LatLonQuad();
-            quad.Coordinates = new CoordinateCollection();
-            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC1.Latitude, implment.GraphicC1.Longitude));
-            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC2.Latitude, implment.GraphicC2.Longitude));
-            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC3.Latitude, implment.GraphicC3.Longitude));
-            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC4.Latitude, implment.GraphicC4.Longitude));
-
-            var folder = new Folder();
-            folder.Id = "f0";
-            folder.Name = "Folder 0";
-
-            var gndOverlay = new GroundOverlay();
-            gndOverlay.Icon = new Icon();
-            gndOverlay.Icon.Href = new Uri("tractor.png", UriKind.Relative);
-            gndOverlay.GXLatLonQuad = quad;
-            folder.AddFeature(gndOverlay);
-
-            // This is the root element of the file
-            Kml kml = new Kml();
-            kml.Feature = folder;
-
-            KmlFile.Create(kml, false);
-
-            Serializer serializer = new Serializer();
-            serializer.Serialize(kml);
-
-            var stream = File.Open(kmlPath,
-                FileMode.CreateNew, FileAccess.ReadWrite);
-
-            using (var writer = new StreamWriter(stream))
-            {
-                writer.Write(serializer.Xml);
-            }
-
-            Uri sourceUri = new Uri(kmlPath);
-            var tractorKml = new KmlLayer(sourceUri);
-            tractorKml.LoadAsync();
-            _mapView.Map.Basemap.BaseLayers.Clear();
-            _mapView.Map.Basemap.BaseLayers.Add(tractorKml);
-        }
-    }

File KmlOverlay.cs Added

  • Ignore whitespace
  • Hide word diff
+using SharpKml.Dom;
+using SharpKml.Dom.GX;
+using SharpKml.Engine;
+using System;
+using System.IO;
+
+namespace FormsMapsShared.ArcGisRendering
+
+public class TractorRenderer
+    {
+        Esri.ArcGISRuntime.Xamarin.Forms.MapView _mapView;
+        string kmlPath = "/storage/emulated/0/SmartMaps/tractor.kml";
+        public TractorRenderer(Esri.ArcGISRuntime.Xamarin.Forms.MapView mapView)
+        {
+            _mapView = mapView;      
+        }
+
+
+        public void UpdateTractorPosition(IImplement implment)
+        {
+            //delete the file from the last update
+            if(File.Exists(kmlPath))
+            {
+                File.Delete(kmlPath);
+            }
+  
+            var quad = new LatLonQuad();
+            quad.Coordinates = new CoordinateCollection();
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC1.Latitude, implment.GraphicC1.Longitude));
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC2.Latitude, implment.GraphicC2.Longitude));
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC3.Latitude, implment.GraphicC3.Longitude));
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC4.Latitude, implment.GraphicC4.Longitude));
+
+            var folder = new Folder();
+            folder.Id = "f0";
+            folder.Name = "Folder 0";
+
+            var gndOverlay = new GroundOverlay();
+            gndOverlay.Icon = new Icon();
+            gndOverlay.Icon.Href = new Uri("tractor.png", UriKind.Relative);
+            gndOverlay.GXLatLonQuad = quad;
+            folder.AddFeature(gndOverlay);
+
+            // This is the root element of the file
+            Kml kml = new Kml();
+            kml.Feature = folder;
+
+            KmlFile.Create(kml, false);
+
+            Serializer serializer = new Serializer();
+            serializer.Serialize(kml);
+
+            var stream = File.Open(kmlPath,
+                FileMode.CreateNew, FileAccess.ReadWrite);
+
+            using (var writer = new StreamWriter(stream))
+            {
+                writer.Write(serializer.Xml);
+            }
+
+            Uri sourceUri = new Uri(kmlPath);
+            var tractorKml = new KmlLayer(sourceUri);
+            tractorKml.LoadAsync();
+            _mapView.Map.Basemap.BaseLayers.Clear();
+            _mapView.Map.Basemap.BaseLayers.Add(tractorKml);
+        }
+    }
Created by Philip O'Gorman

File KmlOverlay Added

  • Ignore whitespace
  • Hide word diff
+using SharpKml.Dom;
+using SharpKml.Dom.GX;
+using SharpKml.Engine;
+using System;
+using System.IO;
+
+namespace FormsMapsShared.ArcGisRendering
+
+public class TractorRenderer
+    {
+        Esri.ArcGISRuntime.Xamarin.Forms.MapView _mapView;
+        string kmlPath = "/storage/emulated/0/SmartMaps/tractor.kml";
+        public TractorRenderer(Esri.ArcGISRuntime.Xamarin.Forms.MapView mapView)
+        {
+            _mapView = mapView;      
+        }
+
+
+        public void UpdateTractorPosition(IImplement implment)
+        {
+            //delete the file from the last update
+            if(File.Exists(kmlPath))
+            {
+                File.Delete(kmlPath);
+            }
+  
+            var quad = new LatLonQuad();
+            quad.Coordinates = new CoordinateCollection();
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC1.Latitude, implment.GraphicC1.Longitude));
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC2.Latitude, implment.GraphicC2.Longitude));
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC3.Latitude, implment.GraphicC3.Longitude));
+            quad.Coordinates.Add(new SharpKml.Base.Vector(implment.GraphicC4.Latitude, implment.GraphicC4.Longitude));
+
+            var folder = new Folder();
+            folder.Id = "f0";
+            folder.Name = "Folder 0";
+
+            var gndOverlay = new GroundOverlay();
+            gndOverlay.Icon = new Icon();
+            gndOverlay.Icon.Href = new Uri("tractor.png", UriKind.Relative);
+            gndOverlay.GXLatLonQuad = quad;
+            folder.AddFeature(gndOverlay);
+
+            // This is the root element of the file
+            Kml kml = new Kml();
+            kml.Feature = folder;
+
+            KmlFile.Create(kml, false);
+
+            Serializer serializer = new Serializer();
+            serializer.Serialize(kml);
+
+            var stream = File.Open(kmlPath,
+                FileMode.CreateNew, FileAccess.ReadWrite);
+
+            using (var writer = new StreamWriter(stream))
+            {
+                writer.Write(serializer.Xml);
+            }
+
+            Uri sourceUri = new Uri(kmlPath);
+            var tractorKml = new KmlLayer(sourceUri);
+            tractorKml.LoadAsync();
+            _mapView.Map.Basemap.BaseLayers.Clear();
+            _mapView.Map.Basemap.BaseLayers.Add(tractorKml);
+        }
+    }
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.