using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { /// <summary> /// 公用的特殊函数 /// </summary> public class SpecialFunctions { #region 构造函数 /// <summary> /// 构造函数 /// </summary> public SpecialFunctions() { } #endregion #region 方法 /// <summary> /// 数字经纬度和度分秒经纬度转换 (Digital degree of latitude and longitude and vehicle to latitude and longitude conversion) /// </summary> /// <param name="digitalLati_Longi">数字经纬度</param> /// <return>度分秒经纬度</return> static public string ConvertDigitalToDegrees(string digitalLati_Longi) { double digitalDegree = Convert.ToDouble(digitalLati_Longi); return ConvertDigitalToDegrees(digitalDegree); } /// <summary> /// 数字经纬度和度分秒经纬度转换 (Digital degree of latitude and longitude and vehicle to latitude and longitude conversion) /// </summary> /// <param name="digitalDegree">数字经纬度</param> /// <return>度分秒经纬度</return> static public string ConvertDigitalToDegrees(double digitalDegree) { const double num = 60; int degree = (int)digitalDegree; double tmp = (digitalDegree - degree) * num; int minute = (int)tmp; double second = (tmp - minute) * num; string degrees = "" + degree + "°" + minute + "′" + second + "″"; return degrees; } /// <summary> /// 度分秒经纬度(必须含有'°')和数字经纬度转换 /// </summary> /// <param name="digitalDegree">度分秒经纬度</param> /// <return>数字经纬度</return> static public double ConvertDegreesToDigital(string degrees) { const double num = 60; double digitalDegree = 0.0; int d = degrees.IndexOf('°'); //度的符号对应的 Unicode 代码为:00B0[1](六十进制),显示为°。 if (d < 0) { return digitalDegree; } string degree = degrees.Substring(0, d); digitalDegree += Convert.ToDouble(degree); int m = degrees.IndexOf('′'); //分的符号对应的 Unicode 代码为:2032[1](六十进制),显示为′。 if (m < 0) { return digitalDegree; } string minute = degrees.Substring(d + 1, m - d - 1); digitalDegree += ((Convert.ToDouble(minute)) / num); int s = degrees.IndexOf('″'); //秒的符号对应的 Unicode 代码为:2033[1](六十进制),显示为″。 if (s < 0) { return digitalDegree; } string second = degrees.Substring(m + 1, s - m - 1); digitalDegree += (Convert.ToDouble(second) / (num * num)); return digitalDegree; } /// <summary> /// 度分秒经纬度(必须含有'/')和数字经纬度转换 /// </summary> /// <param name="digitalDegree">度分秒经纬度</param> /// <param name="cflag">分隔符</param> /// <return>数字经纬度</return> static public double ConvertDegreesToDigital_default(string degrees) { char ch = '/'; return ConvertDegreesToDigital(degrees, ch); } /// <summary> /// 度分秒经纬度和数字经纬度转换 /// </summary> /// <param name="digitalDegree">度分秒经纬度</param> /// <param name="cflag">分隔符</param> /// <return>数字经纬度</return> static public double ConvertDegreesToDigital(string degrees, char cflag) { const double num = 60; double digitalDegree = 0.0; int d = degrees.IndexOf(cflag); if (d < 0) { return digitalDegree; } string degree = degrees.Substring(0, d); digitalDegree += Convert.ToDouble(degree); int m = degrees.IndexOf(cflag, d + 1); if (m < 0) { return digitalDegree; } string minute = degrees.Substring(d + 1, m - d - 1); digitalDegree += ((Convert.ToDouble(minute)) / num); int s = degrees.Length; if (s < 0) { return digitalDegree; } string second = degrees.Substring(m + 1, s - m - 1); digitalDegree += (Convert.ToDouble(second) / (num * num)); return digitalDegree; } #endregion } }