Parsing local gpx file in Android
Asked Answered
P

4

8

I followed this example to parse a local GPX file in Android: http://android-coding.blogspot.pt/2013/01/get-latitude-and-longitude-from-gpx-file.html

All works fine to access "lat" and "long" but I need also to get the "ele" value but all my tentatives were unsuccessful. Anyone can give me some hits to do that?

Thanks in advance! Best regards, NR.

Penick answered 6/9, 2013 at 14:59 Comment(0)
C
4

you have the "Node node = nodelist_trkpt.item(i);" in your first loop. Get the child elements from this node an run through these child elements.

e.g.:

NodeList nList = node.getChildNodes();
for(int j=0; j<nList.getLength(); j++) {
    Node el = nList.item(j);
    if(el.getNodeName().equals("ele")) {
     System.out.println(el.getTextContent());
  }
}
Caylacaylor answered 6/9, 2013 at 15:27 Comment(0)
F
5

I will add my library for GPX parsing to these answers: https://github.com/ticofab/android-gpx-parser. It provides two ways to parse you GPX file: once you obtain / create a GPXParser object (mParser in the examples below), you can then either parse directly your GPX file

Gpx parsedGpx = null;
try {
    InputStream in = getAssets().open("test.gpx");
    parsedGpx = mParser.parse(in);
} catch (IOException | XmlPullParserException e) {
    e.printStackTrace();
}
if (parsedGpx == null) {
    // error parsing track
} else {
    // do something with the parsed track
}

or you can parse a remote file:

mParser.parse("http://myserver.com/track.gpx", new GpxFetchedAndParsed() {
    @Override
    public void onGpxFetchedAndParsed(Gpx gpx) {
        if (gpx == null) {
            // error parsing track
        } else {
            // do something with the parsed track
        }
    }
});

Contributions are welcome.

Felodese answered 24/1, 2016 at 11:9 Comment(2)
can you tell me how to parse extensions in GPx file using your libraryThirzi
hey @Thirzi I have seen your issue on GitHub, but haven't had time to work on it yet. Feel free to add the functionality and open a pull request :)Felodese
C
4

you have the "Node node = nodelist_trkpt.item(i);" in your first loop. Get the child elements from this node an run through these child elements.

e.g.:

NodeList nList = node.getChildNodes();
for(int j=0; j<nList.getLength(); j++) {
    Node el = nList.item(j);
    if(el.getNodeName().equals("ele")) {
     System.out.println(el.getTextContent());
  }
}
Caylacaylor answered 6/9, 2013 at 15:27 Comment(0)
S
1

Update: I've added parsing "ele" element as well, so this code could match your requirements.

I will propose different approach: https://gist.github.com/kamituel/6465125.

In my approach I don't create an ArrayList of all track points (this is done in the example you posted). Such a list can consume quite a lot of memory, which can be an issue on Android.

I've even given up on using regex parsing to avoid allocating too many objects (which causes garbage collector to run).

As a result, running Java with 16Mb heap size, parsing GPX file with over 600 points, garbage collector will be run only 12 times. I'm sure one could go lower, but I didn't optimize it heavily yet.

Usage:

GpxParser parser = new GpxParser(new FileInputStream(file));
TrkPt point = null;
while ((point = parser.nextTrkPt()) != null) {
  // point.getLat()
  // point.getLon()
}

I've successfully used this code to parse around 100 Mb of GPX files on Android. Sorry it's not in the regular repo, I didn't plan to share it just yet.

Squint answered 6/9, 2013 at 15:15 Comment(0)
M
1

I've ported the library GPXParser by ghitabot to Android.

https://github.com/urizev/j4gpx

Mummify answered 27/5, 2015 at 8:41 Comment(4)
how to use in studio?Thirzi
@Thirzi Add the jar file to the libs folderMummify
this library will supports extensions in gpx fileThirzi
@Thirzi I don't know if that is a question or an afirmation. I will assume it is a question. I'm not sure if the library supports extensions because I ported it from a SourceForge project and I fixed some issues. However I haven't ever use extensions with the library. If you wish you could check it out in the code.Mummify

© 2022 - 2024 — McMap. All rights reserved.