/// <summary> /// Converts a latitude or longitude coordinate to a string. /// </summary> static string FormatLatLon(double coordinate, CoordinateType coordinateType, LatLonFormats latLonFormat) { bool isNegative = coordinate < 0; string coordStr = ""; switch (latLonFormat) { case LatLonFormats.DegreesMinutesSeconds: default: { coordinate = Math.Abs(coordinate); string heading = coordinateType == CoordinateType.Latitude ? (isNegative ? "S" : "N") : (isNegative ? "W" : "E"); double d = Math.Floor(coordinate); double m = Math.Floor((coordinate - d) * 60); double s = Math.Round((((coordinate - d) * 60) - m) * 60); coordStr = string.Format("{0}°{1}'{2}\"{3}", d.ToString(), m.ToString().PadLeft(2, '0'), s.ToString().PadLeft(2, '0'), heading); break; } case LatLonFormats.DegreesMinutesDecSeconds: { coordinate = Math.Abs(coordinate); string heading = coordinateType == CoordinateType.Latitude ? (isNegative ? "S" : "N") : (isNegative ? "W" : "E"); double d = Math.Floor(coordinate); double m = Math.Floor((coordinate - d) * 60); double s = Math.Round((((coordinate - d) * 60) - m) * 60, 2); coordStr = string.Format("{0}°{1}'{2}\"{3}", d.ToString(), m.ToString().PadLeft(2, '0'), s.ToString().PadLeft(2, '0'), heading); break; } case LatLonFormats.DecimalDegrees: { coordStr = string.Format("{0}°", Math.Round(coordinate, 6).ToString()); break; } } return(coordStr); }
/// <summary> /// Formats the lat/lon values of the specified map point. /// </summary> ///<remarks>The Y value is interpreted as latitude, the X value as longitude.</remarks> public static void GetLatLonStrings(MapPoint mapPoint, out string lat, out string lon, LatLonFormats latLonFormat = LatLonFormats.DegreesMinutesSeconds) { lat = FormatLatLon(mapPoint.Y, CoordinateType.Latitude, latLonFormat); lon = FormatLatLon(mapPoint.X, CoordinateType.Longitude, latLonFormat); }