经纬度、时分秒转换的C#类

你说的曾经没有我的故事 提交于 2020-03-28 02:39:53

一:新建一个包含经纬度、时间转换的类optaDataConvert

public class optaDataConvert
    {


        /// <summary>  
        /// 时间转换  
        /// </summary>  
        /// <param name="value"></param>  
        /// <returns></returns>  
        public static DateTime GetDataTime(long value)
        {
            return new DateTime(1970, 1, 1).AddMilliseconds(value).AddHours(8);
        }
        /// <summary>  
        /// 获取经度  
        /// </summary>  
        /// <returns></returns>  
        public static string GetLongitudeValue(decimal value)
        {
            ArrayList arrList = LgetValid(value);
            if (arrList == null)
                return string.Empty;
            if (arrList.Count != 3)
                return string.Empty;
            return string.Format("{0}°{1}.{2}' {3}", arrList[0], arrList[1], arrList[2], value > 0 ? "E" : "W");
        }
        /// <summary>  
        /// 获取纬度  
        /// </summary>  
        /// <returns></returns>  
        public static string GetLatitudeValue(decimal value)
        {
            ArrayList arrList = LgetValid(value);
            if (arrList == null)
                return string.Empty;
            if (arrList.Count != 3)
                return string.Empty;
            return string.Format("{0}°{1}.{2}' {3}", arrList[0], arrList[1], arrList[2], value > 0 ? "N" : "S");
        }
        /// <summary>  
        /// 获取经纬度度分秒  
        /// </summary>  
        /// <param name="value"></param>  
        /// <returns></returns>  
        public  static ArrayList LgetValid(decimal value)
        {
            ArrayList aList = new ArrayList();
            string excute = ChangeValue(value);
            string[] sArray = excute.Split('|');
            aList.Insert(0, sArray[0]);
            aList.Insert(1, sArray[1]);
            aList.Insert(2, sArray[2]);
            return aList;
        }
        /// <summary>  
        /// 经纬度转换  
        /// </summary>  
        /// <param name="str">经度</param>  
        /// <param name="num">保留8位值</param>  
        /// <returns></returns>  
        protected static string ChangeValue(decimal degree)
        {
            if (degree < 0)
            {
                degree = -degree;
            }
            decimal minute = (degree - (int)degree) * 60;
            decimal second = (minute - (int)minute) * 60;
            return ((int)degree).ToString() + "|" + ((int)minute).ToString() + "|" + ((int)second).ToString();
        }
        public static decimal TimeToValue(string Degrees, string Minutes, string Seconds)
        {
            decimal Value = decimal.Parse(Minutes) + decimal.Parse(Minutes) / 60 + decimal.Parse(Seconds) / 3600;
            return Value;
        }
    }

}

二:调用该函数

1.把时分秒转换成经纬度存储

 m_cardpoint.X = optaDataConvert.TimeToValue(X_Du.Text, X_Fen.Text, X_Miao.Text);
 m_cardpoint.Y = optaDataConvert.TimeToValue(Y_Du.Text, Y_Fen.Text, Y_Miao.Text);

(其中m_cardpoint.X 、m_cardpoint.Y 为存储经纬度的值)

2.把经纬度转换成时分秒展示在前台

ArrayList arrlistLatitude = optaDataConvert.LgetValid(m_cardpoint.Y);
           Y_Du.Text = arrlistLatitude[0].ToString();
           Y_Fen.Text = arrlistLatitude[1].ToString();
           Y_Miao.Text = arrlistLatitude[2].ToString();

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!