Parsing local gpx file in Android

前端 未结 4 1447
闹比i
闹比i 2021-01-02 11:10

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

4条回答
  •  别那么骄傲
    2021-01-02 11:46

    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.

提交回复
热议问题