一般从GPS得到的数据是经纬度。经纬度有多种表示方法。
1.) ddd.ddddd, 度 . 度的十进制小数部分(5位)例如:31.12035º
2.) ddd.mm.mmm,度 . 分 . 分的十进制小数部分(3位)例如 31º10.335′
3.) ddd.mm.ss, 度 . 分 . 秒 例如 31º12’42″
地球上任何一个固定的点都可以用确定的经纬度表示出来。
关于经纬度坐标转换的方法
一、十进制转换成经纬度
把经纬度转换成十进制的方法很简单
如下就可以了
Decimal Degrees = Degrees + minutes/60 + seconds/3600
例:57°55’56.6″ =57+55/60+56.6/3600=57.9323888888888
114°65’24.6″=114+65/60+24.6/3600=结果自己算!
如把经纬度 (longitude,latitude) (205.395583333332,57.9323888888888)
转换据成坐标(Degrees,minutes,seconds)(205°23’44.1″,57°55’56.6″)。
步骤如下:
1, 直接读取”度”:205
2,(205.395583333332-205)*60=23.734999999920 得到”分”:23
3,(23.734999999920-23)*60=44.099999995200 得到”秒”:44.1
采用同样的方法可以得到纬度坐标:57°55’56.6″
办法很多,三维字符数组,指针字符数组都可以,分别保存字符串到数组元素,最后以%s输出即可。我来写个最简单的
#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
void explain_NS(char * str)
{
char tmp_ca[30] = ""
if(str[0] == 'N')
strcpy(tmp_ca,"北纬")
else
strcpy(tmp_ca,"南纬")
char *p = tmp_ca
while(*p) p++
strncpy(p, str+1, 2)
p += 2
*p = 176
p ++
strncpy(p,str+3,2)
p += 2
*p = 39
p++
strncpy(p, str+6,5)
p += 5
*p = 34
printf("%s\n",tmp_ca)
}
void explain_EW(char * str)
{
char tmp_ca[30] = ""
if(str[0] == 'E')
strcpy(tmp_ca,"东经")
else
strcpy(tmp_ca,"西经")
char *p = tmp_ca
while(*p) p++
strncpy(p, str+1, 3)
p += 3
*p = 176
p ++
strncpy(p,str+4,2)
p += 2
*p = 39
p++
strncpy(p, str+7,5)
p += 5
*p = 34
printf("%s\n",tmp_ca)
}
int _tmain(int argc, _TCHAR* argv[])
{
char ca[30] = "N3018.93661"
char cb[30] = "E12022.88281"
explain_NS(ca)
explain_EW(cb)
system("pause")
return 0
}
#include <stdio.h>#include <math.h>
#define deg2rad 0.01745329251994433
struct Data
{
double X, Y, Z
double B, L, H
double a
double b
}
void BLH2XYZ(Data *dsrc)
{
double e = sqrt(1 - (dsrc->b / dsrc->a)*(dsrc->b / dsrc->a))
double sinB = sin(dsrc->B)
double cosB = cos(dsrc->B)
double sinL = sin(dsrc->L)
double cosL = cos(dsrc->L)
double tanB_2 = tan(dsrc->B) * tan(dsrc->B)
double f = 1 - e * e
double fd = sqrt(1 + f * tanB_2)
dsrc->X = (dsrc->a * cosL) / fd + dsrc->H * cosB * cosL
dsrc->Y = (dsrc->a * sinL) / fd + dsrc->H * cosB * sinL
double fd2 = sqrt(1 - e * e * sinB * sinB)
dsrc->Z = (dsrc->a * f * sinB) / fd2 + dsrc->H * sinB
}
int main()
{
Data dsrc
dsrc.a = 6378137.0000 dsrc.b = 6356752.3142
dsrc.B = 30 * deg2rad dsrc.L = 114 * deg2rad dsrc.H = 20
BLH2XYZ(&dsrc)
printf("%.4lf %.4lf %.4lf\n", dsrc.B, dsrc.L, dsrc.H)
printf("%.4lf %.4lf %.4lf\n", dsrc.X, dsrc.Y, dsrc.Z)
}
结果
上面的结果是我拿别人工具源码算的 下面是程序运行结果 精度只打印了4位有效数字