public double DistanceTo(double lat, double lon) { var src = new Geo.Coordinate(Latitude, Longitude); var dest = new Geo.Coordinate(lat, lon); var path = new Geo.Geometries.LineString(src, dest); return(path.GetLength() .ConvertTo(Geo.Measure.DistanceUnit.Km) .Value); }
/// <summary> /// Creates a kmlLookAt from a Bounds object. /// </summary> /// <param name="ge">the plugin</param> /// <param name="bounds">the bounds object to create the view of</param> /// <param name="aspectRatio">Optional aspect ratio</param> /// <param name="defaultRange">Optional default range</param> /// <param name="scaleRange">Optional scale range</param> /// <returns>A KmlLookAt based on the <paramref name="bounds"/> (or null)</returns> public static dynamic CreateBoundsView( dynamic ge, Bounds bounds, double aspectRatio = 1.0, double defaultRange = 1000, double scaleRange = 1.5) { Coordinate center = bounds.Center(); Coordinate boundsSpan = bounds.Span(); double lookAtRange = defaultRange; dynamic lookat = null; if (Convert.ToBoolean(boundsSpan.Latitude) || Convert.ToBoolean(boundsSpan.Longitude)) { // Distance - using law of cosines for speed... double distEW = new Geo.Coordinate(center.Latitude, bounds.East) .Distance(new Geo.Coordinate(center.Latitude, bounds.West)); double distNS = new Geo.Coordinate(bounds.North, center.Longitude) .Distance(new Geo.Coordinate(bounds.South, center.Longitude)); aspectRatio = Math.Min(Math.Max(aspectRatio, distEW / distNS), 1.0); // Create a LookAt using the experimentally derived distance formula. double alpha = Maths.ConvertDegreesToRadians((45.0 / (aspectRatio + 0.4) - 2.0)); double expandToDistance = Math.Max(distNS, distEW); double beta = Math.Min( Maths.ConvertDegreesToRadians(90), alpha + expandToDistance / (2 * Maths.EarthMeanRadiusKilometres)); lookAtRange = scaleRange * Maths.EarthMeanRadiusKilometres * (Math.Sin(beta) * Math.Sqrt(1 + 1 / Math.Pow(Math.Tan(alpha), 2)) - 1); } try { lookat = ge.createLookAt(string.Empty); lookat.set(center.Latitude, center.Longitude, bounds.Top, bounds.Northeast.AltitudeMode, 0, 0, lookAtRange); } catch (RuntimeBinderException rbex) { Debug.WriteLine("CreateBoundsView: " + rbex.Message, "KmlHelpers"); } return lookat; }