public void TestCalculateLookAt() { Placemark placemark = null; Assert.That(() => placemark.CalculateLookAt(), Throws.TypeOf <ArgumentNullException>()); placemark = new Placemark(); Assert.That(placemark.CalculateLookAt(), Is.Null); // Nothing to look at var point = new Point { Coordinate = new Vector(37, -122) }; placemark.Geometry = point; LookAt lookat = placemark.CalculateLookAt(); Assert.That(lookat, Is.Not.Null); Assert.That(lookat.Latitude, Is.EqualTo(37.0)); Assert.That(lookat.Longitude, Is.EqualTo(-122.0)); Assert.That(lookat.Range, Is.EqualTo(1000.0)); Assert.That(lookat.AltitudeMode, Is.EqualTo(AltitudeMode.RelativeToGround)); Assert.That(lookat.Altitude, Is.Null); Assert.That(lookat.Heading, Is.Null); Assert.That(lookat.Tilt, Is.Null); var line = new LineString { Coordinates = new CoordinateCollection { new Vector(37, -122), new Vector(38, -121) } }; placemark = new Placemark { Geometry = line }; lookat = placemark.CalculateLookAt(); Assert.That(lookat.Latitude, Is.EqualTo(37.5)); Assert.That(lookat.Longitude, Is.EqualTo(-121.5)); Assert.That(lookat.Range, Is.EqualTo(135123.4361).Within(0.0001)); }
public static string getKMLstring(Color lapColor, List <double> latitudes, List <double> longitudes, List <double> altitudes = null) { // make sure the parameters are of the same length if (((altitudes != null) && !(latitudes.Count == longitudes.Count && altitudes.Count == longitudes.Count)) || !(latitudes.Count == longitudes.Count)) { throw new Exception("The length of the parameters must match"); } // Create the KML stuff Kml kml = new Kml(); kml.AddNamespacePrefix("gx", "http://www.google.com/kml/ext/2.2"); Document doc = new Document(); Style style = new Style(); Placemark pMark = new Placemark(); LineString ls = new LineString(); ls.Coordinates = new CoordinateCollection(); // Define the style style.Line = new LineStyle(); style.Id = "redline"; style.Line.Color = new Color32(lapColor.A, lapColor.B, lapColor.G, lapColor.R); //style.Line.ColorMode = ColorMode.Normal; style.Line.Width = 2; // Define a new style Style style2 = new Style(); style2.Line = new LineStyle(); style2.Id = "blueline"; style2.Line.Color = new Color32(Colors.LightBlue.A, Colors.LightBlue.B, Colors.LightBlue.G, Colors.LightBlue.R); //style2.Line.ColorMode = ColorMode.Normal; style2.Line.Width = 2; // add style to placemark pMark.StyleUrl = new Uri("#redline", UriKind.Relative); // loop through the lines in the file if (altitudes != null) { ls.AltitudeMode = AltitudeMode.Absolute; for (int i = 0; i < latitudes.Count; i++) { ls.Coordinates.Add(new Vector(latitudes[i], longitudes[i], altitudes[i])); } } else // no altitude provided { for (int i = 0; i < latitudes.Count; i++) { ls.Coordinates.Add(new Vector(latitudes[i], longitudes[i])); } } // Set properties on the line string ls.Extrude = false; ls.Tessellate = true; // Add the line string to the placemark pMark.Geometry = ls; // Generate a LookAt object to center the view on the placemark doc.Viewpoint = pMark.CalculateLookAt(); // Add the placemark and style to the document doc.AddFeature(pMark); doc.AddStyle(style); doc.AddStyle(style2); // Add the document to the kml object kml.Feature = doc; // Create the KML file KmlFile kmlFile = KmlFile.Create(kml, false); //TODO for debugging purposes, save the kml file to test folder. kmlFile.Save("test.kml"); // Return the KML file as a string return(kmlFile.SaveString()); }