• C#: 数字经纬度和度分秒经纬度间的转换


    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
        }
    }
    
  • 相关阅读:
    用DD-WRT自建计费WiFi热点
    docker安全最佳实践概述
    2014年8月25日,收藏家和杀手——面向对象的C++和C(一)
    Maven
    做QA的日子——iOS測试入门(四)
    小贝_mysql select连接查询
    FFmpeg源码简单分析:结构体成员管理系统-AVOption
    Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享
    Redis主从配置及通过Keepalived实现Redis自动切换高可用
    CentOS 安装jdk1.7 32位
  • 原文地址:https://www.cnblogs.com/shenchao/p/3773007.html
Copyright © 2020-2023  润新知