char  GPS1[] = "$GNGGA,121252.000,3937.3032,N,11611.6046,E,1,05,2.0,45.9,M,-5.7,M,,0000*77";

int Parse_GPS(char *data,char * longtitude,char * latitude)
{
    int ret,temp;
    char* start, *tempStr;
    char *field;
   int index = 0;
    start = strstr(data,"GNGGA");

    if (start)
    {
        tempStr = strstr(start,"E");
        if (tempStr == NULL)
        {
            //return -1;
        }

        //*(tempStr - 1) = 0;

        field = strtok(start,",");

        while (field)
        {
            index++;
            printf("%s\n", field);
            field = strtok(NULL,",");
            if (index == 2)
            {
                strcpy(longtitude,field);
            }
            if (index == 4)
            {
                strcpy(latitude, field);
            }
        }
    }

    return 0;
}

int main(int argc, char *argv[])
{
	int ret;
	int port = -1;
    CHAR * server = (CHAR*)"N/A";
	if(argc == 2)
	{
		port = atoi(argv[1]);
		if(port >65535 || port <1)
		{
			printf("Port should between 1-65535!\n");
			return 0;
		}
	}


    char longtitude[100] = { 0 };
    char latitude[100] = { 0 };

    ret = Parse_GPS(GPS1, longtitude, latitude);

    printf("[%s----%s]\n", longtitude, latitude);
    char* temp = strstr(longtitude,".");
    char dummy1,dummy2;
    if (temp)
    {
        dummy1 = *(temp - 1);
        dummy2 = *(temp - 2);

        *temp = dummy1;
        *(temp - 1) = dummy2;
        *(temp - 2) = '.';
    }


    temp = strstr(latitude, ".");
    
    if (temp)
    {
        dummy1 = *(temp - 1);
        dummy2 = *(temp - 2);

        *temp = dummy1;
        *(temp - 1) = dummy2;
        *(temp - 2) = '.';
    }



	
    printf("[%s----%s]\n",longtitude, latitude);


    double f_long = atof(longtitude);
    double f_lati = atof(latitude);

    printf("[%f----%f]\n", f_long, f_lati);

    return 0;

}

刚开始不知道 后来经过实测发现  gps模块直接读出来的数据 是需要转换的 

转换公式可以去看下gps的手册上一般会提供的

代码很简单 如下:

double longtitude = atof("11403.2467");
    double latitude = atof("2236.9142");


    double long1 = (int)(longtitude / 100) + fmod(longtitude, 100) / 60.0;

    double lati = (int)(latitude / 100) + fmod(latitude, 100) / 60.0;

不了解业务的前提下 写代码 就是 聪明反被聪明误!!!!!

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐