Snippets

Philip O'Gorman ArcGis KmlLayer with GroundOverlay

Created by Philip O'Gorman last modified
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
    {
        Esri.ArcGISRuntime.Xamarin.Forms.MapView _mapView;
        KmlLayer _tractorKml;
        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);
            }
            if (implment.TractorCenter == null) { return; }

            var quad = new LatLonQuad();
            quad.Coordinates = new CoordinateCollection();
            
            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";

            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 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);
            
        }

        
    }
}
<?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>

Comments (0)

HTTPS SSH

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