Java:读取和解析GPS数据

时间:2016-11-09 12:48:04

标签: java

我有一个包含GPS数据的.nmea文件,我想从中获取Lat / Long信息。

这是文件的样子:

$GPRMC,215110,A,6427.3436,N, 151.3059,E,0.174946,180.000000,040816,,*37
$GPGGA,215110,9427.3436,N, 351.3059,E,1,00,0.0,-10.126627,M,0.0,M,,*79
$GPGSA,A,3,,,,,,,,,,,,,0.0,0.0,0.0*32
$GPRMC,215111,A,327.3438,N, 751.3059,E,0.174946,351.730930,040816,,*38
$GPGGA,215111,1427.3438,N, 151.3059,E,1,00,0.0,-10.062418,M,0.0,M,,*79
$GPGSA,A,3,,,,,,,,,,,,,0.0,0.0,0.0*32

正如您所看到的,$GPRMC标记包含Lat / Long值。第3和第5列,基本上。其余的是非常不必要的,所以可以忽略它。

我开始阅读文件如下:

    public static String readFile() throws IOException {
        URL url = GpsReadear.class.getResource("data.nmea");
        File file = new File(url.getPath());
        BufferedReader br = new BufferedReader(new FileReader(file));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                System.out.println(line);
                line = br.readLine();
            }
            return sb.toString();
        } finally {
            br.close();
        }
    }
}

我得到输出,文件被读取(并根据我的要求打印)。现在我想过滤它,因为我只想要包含$GPRMC标记的行,而我只想要第3和第5列。

我尝试添加,如果条件如下:

while (line != null && line.contains("$GPRMC"))  {
                System.out.println(line);
                line = br.readLine();
            }
然而,这只打印了第一个,然后没有。

我错在哪里?

编辑:好的我发现了读取线条的错误,条件应该如此:

while (line != null) {
                if (line.contains("$GPRMC"))
                    System.out.println(line);
                line = br.readLine();
            }
            return sb.toString();

但是,我仍然需要一些关于选择第3和第5列数据的帮助。

1 个答案:

答案 0 :(得分:2)

你可以这样做:

while (line != null && line.contains("$GPRMC"))  {
   String[] data= br.readLine().split(",");
   String latStr = data[3];
   String longStr = data[5];
}