Java 如何将NMEA格式数据的经纬度转换为十进制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36254363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:38:07  来源:igfitidea点击:

How to convert latitude and longitude of NMEA format data to decimal?

javaphpnmea

提问by NajLinus

I have latitude and longitude of NMEA format, and I want to convert it into decimal value. Is there any formula? For example, NMEA format Latitude = 35.15 N and Longitude = 12849.52 E

我有NMEA格式的经纬度,我想把它转换成十进制值。有什么公式吗?例如,NMEA 格式纬度 = 35.15 N 和经度 = 12849.52 E

采纳答案by johnDoe

The format for NMEA coordinates is (d)ddmm.mmmm
d=degrees and m=minutes
There are 60 minutes in a degree so divide the minutes by 60 and add that to the degrees.

NMEA 坐标的格式为 (d)ddmm.mmmm
d=degrees 和 m=minutes
一个度数有 60 分钟,因此将分钟数除以 60 并将其添加到度数中。

For the Latitude=35.15 N
35.15/60 = .5858 N

对于纬度=35.15 N
35.15/60 = .5858 N

For the Longitude= 12849.52 E,
128+ 49.52/60 = 128.825333 E

对于经度= 12849.52 E,
128+ 49.52/60 = 128.825333 E

In php, you could do this:

在 php 中,你可以这样做:

<?php
$lng = "12849.52 W";

$brk = strpos($lng,".") - 2;
if($brk < 0){ $brk = 0; }

$minutes = substr($lng, $brk);
$degrees = substr($lng, 0,$brk);

$newLng = $degrees + $minutes/60;

if(stristr($lng,"W")){
    $newLng = -1 * $newLng;
}

?>

回答by xrgb

Yes, NMEA format is ddmm.mmmm, n/s (d)ddmm.mmmm, e/w

是的,NMEA 格式为 ddmm.mmmm、n/s (d)ddmm.mmmm、e/w

To get to decimal degrees from degrees ad minutes, you use the following formula:

要从度数和分钟数到十进制度数,您可以使用以下公式:

(d)dd + (mm.mmmm/60) (* -1 for W and S)

(d)dd + (mm.mmmm/60)(* -1 代表 W 和 S)

There is a nice little calculator here: http://www.hiddenvision.co.uk/ez/

这里有一个不错的小计算器:http: //www.hiddenvision.co.uk/ez/

回答by hamboy75

This is for small devices where double values are a problem. It is done in c code but can be changed to another language easily:

这适用于存在双值问题的小型设备。它是用 c 代码完成的,但可以轻松地更改为另一种语言:

void GetGPSPos(char *str,char *NMEAgpspos,uint8_t sign)
{
  unsigned short int u=0,d=0;
  unsigned int minutes;
  unsigned char pos,i,j;

  for(pos=0;pos<strlen(NMEAgpspos) && NMEAgpspos[pos]!='.';pos++);

  for(i=0;i<pos-2;i++)
    {
     u*=10;
     u+=NMEAgpspos[i]-'0';
    }
  d=(NMEAgpspos[pos-2]-'0')*10;
  d+=(NMEAgpspos[pos-1]-'0');
  for(i=pos+1,j=0;i<strlen(NMEAgpspos) && j<4;i++,j++) //Only 4 chars
    {
     d*=10;
     d+=NMEAgpspos[i]-'0';
    }


  minutes=d/60;
  sprintf(str,"%d.%04d",(sign?-1:1)*u,minutes);
}

if you dont have sprintf or it doesn't allow "%04" (like it is my case), just change the sprintf line for:

如果您没有 sprintf 或者它不允许“%04”(就像我的情况),只需将 sprintf 行更改为:

  pos=0;
  if(sign)
    str[pos++]='-';
  if(u>100)
    str[pos++]=u/100+'0';
  if(u>10)
    str[pos++]=u/10%10+'0';
  str[pos++]=u%10+'0';
  str[pos++]='.';
  str[pos++]=minutes/1000+'0';
  str[pos++]=minutes/100%10+'0';
  str[pos++]=minutes/10%10+'0';
  str[pos++]=minutes%10+'0';
  str[pos++]=0;