`

经纬度的度数和十进制数互相转换工具

    博客分类:
  • Map
阅读更多

在使用Google Maps的时候,需要根据经纬度进行定位,但手上的经纬度信息都是标准度数表示的,还需要转换成十进制,使用计算机算了一下,感觉比较麻烦,加上好久没有 编程序了,一时手痒,写了一个经纬度的度数和十进制数转换工具,希望能提高一下效率,这里共享如下:

初始界面:

运行效果界面:

错误处理信息:

软件下载地址

softbbs.it168.com/viewthread.php

 

对于源码,如果需要可以留言,其中算法处理代码如下:


//度数转换到十进制数的处理函数
//nType:标识经纬度类型,主要是为了处理参数校验的告警信息 0:进度,1:纬度
//str:经纬度的度数字符串(以逗号分隔,例: 100,2,4 - 100度2分4秒)
double CLDChangeDlg::Degree2Double(int nType, CString str)
{
    CString dstr;
    int nPos,nPos2;
    int num;
    double d = 0.0;

    nPos = str.Find(',');
    if(nPos == -1)
    {
        num = atoi((LPCTSTR)str);
        if(nType == 0 && (num < 0 || num >= 180))
        {
            MessageBox("经度参数错误:度数必须是介于0~180的值!");
            return d;
        }
        else if (nType == 1 && (num < 0 || num >= 90))
        {
            MessageBox("维度参数错误:度数必须是介于0~90的值!");
            return d;
        }
        d += num;       
        return d;
    }
   
    dstr = str.Mid(0, nPos);
    num = atoi((LPCTSTR)dstr);
    if(nType == 0 && (num < 0 || num >= 180))
    {
        MessageBox("经度参数错误:度数必须是介于0~180的值!");
        return d;
    }
    else if (nType == 1 && (num < 0 || num >= 90))
    {
        MessageBox("维度参数错误:度数必须是介于0~90的值!");
        return d;
    }
    d += num;

    nPos++;
    nPos2 = str.Find(',', nPos);
    if(nPos == -1)
    {
        dstr = str.Mid(nPos);
        num = atoi((LPCTSTR)str);
        if(num < 0 || num >= 60)
        {
            if(nType == 0)
            {
                MessageBox("经度参数错误:分数必须是介于0~60的值!");
                return d;
            }
            else if (nType == 1)
            {
                MessageBox("维度参数错误:分数必须是介于0~60的值!");
                return d;
            }
            return d;
        }
        d += (double)num/(double)60.0;       
        return d;
    }

    dstr = str.Mid(nPos, nPos2-nPos);
    num = atoi((LPCTSTR)dstr);
    if(num < 0 || num >= 60)
    {
        if(nType == 0)
        {
            MessageBox("经度参数错误:分数必须是介于0~60的值!");
            return d;
        }
        else if (nType == 1)
        {
            MessageBox("维度参数错误:分数必须是介于0~60的值!");
            return d;
        }
        return d;
    }
    d += (double)num/(double)60.0;

    nPos2++;
    nPos = str.Find(',', nPos2);
    if(nPos == -1)
    {
        dstr = str.Mid(nPos2);
        num = atoi((LPCTSTR)dstr);
        if(num < 0 || num >= 60)
        {
            if(nType == 0)
            {
                MessageBox("经度参数错误:秒数必须是介于0~60的值!");
                return d;
            }
            else if (nType == 1)
            {
                MessageBox("维度参数错误:秒数必须是介于0~60的值!");
                return d;
            }
            return d;
        }
        d += (double)num/(double)3600.0;       
        return d;
    }

    dstr = str.Mid(nPos);
    num = atoi((LPCTSTR)dstr);
    if(num < 0 || num >= 60)
    {
        if(nType == 0)
        {
            MessageBox("经度参数错误:秒数必须是介于0~60的值!");
            return d;
        }
        else if (nType == 1)
        {
            MessageBox("维度参数错误:秒数必须是介于0~60的值!");
            return d;
        }
        return d;
    }
    d += (double)num/(double)3600.0;

    return d;
}

//十进制数转换到度数的处理函数
//num:经纬度的double十进制数
//pstr:转换后的度数字符串(以逗号分隔,例: 100,2,4 - 100度2分4秒)
void CLDChangeDlg::Double2Degree(double num, char * pstr)
{
    char str[50];
    int n;
    double d = num;

    strcpy(pstr,"");

    n = (int)d;
    sprintf(str,"%d,", n);
    strcat(pstr, str);


    d = double(d - n)*60;
    n = (int)d;
    sprintf(str,"%d,", n);
    strcat(pstr, str);

    d = double(d - n)*60;
    n = (int)d;
    sprintf(str,"%d", n);
    strcat(pstr, str);


    return;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics