Processing GPX data with Flex

GPX files that you capture from your GPS device using GPSBabel are in XML and start with the following tags:


<gpx
  version="1.0"
  creator="GPSBabel - http://www.gpsbabel.org"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.topografix.com/GPX/1/0"
  xsi:schemaLocation="http://www.topografix.com/GPX/1/0http://www.topografix.com/GPX/1/0/gpx.xsd"
  >
  ...

You wont be able to read these GPX files from Flex (2.01) without modifying the namespace definition. Here is the problem that you will run into: when you try to get all of the trkpt elements, using a construct such as gpx..trkpt (or any other method of traversing the XML, e.g., gpx.descendants("trkpt")) the ActionScript program will silently fail. The only to way to get around this is to remove the XML name space declaration

xmlns="http://www.topografix.com/GPX/1/0"

from the start of your XML file. This must be a bug in ActionScript. The same GPX file works fine when you read it from DHTML using Google AJAX mapping API.

Advertisement

13 Responses to “Processing GPX data with Flex”

  1. Bike Trails in the Route Player « n o t e 1 9 . c o m Says:

    [...] You’ll need to edit the GPX namespace specification before you try to use the [...]

  2. Jes Says:

    I ran into this same problem… Did you ever find a fix? It seems a shame to have to modify a GPX file to process it…

    Thanks!

    - Jes

  3. sj Says:

    I’ve not really looked for a proper fix, I’m afraid. If you do find on, please do let me know.

  4. sj Says:

    Defining the namespace in ActionScript program, may help, but I’ve not tried it.

  5. da Says:

    Defining the namespace does work, here’s an example….

    namespace topo = “http://www.topografix.com/GPX/1/0″;
    use namespace topo;
    for each (var trkpt:XML in gpx..trkpt) {

  6. sj Says:

    Perfect. Thank you for the update.

  7. Loocie Says:

    I had the same Problem too. I use the XML-Object as dataprovider in a datagrid. After including the namespace (topo), like in da’s example it seams to work. But it is not possible to read elements inside the -elements like or . Only the attributes @lon + @lat inside the are readable. When I remove the namespace declaration xmlns=”http://www.topografix.com/GPX/1/0″ all sub elements are accessible.

  8. Brian Says:

    I’m having trouble getting flex to read the gpx even with the new namespace. I’m using HTTPService to read the gpx in e4x resultFormat. I still get “a term is undefined and has no properties” back. Here is the basic code…

    XML.ignoreWhitespace = true;
    namespace topo = “http://www.topografix.com/GPX/1/0″;
    use namespace topo;
    var gpx:XML = XML(e.result);
    outputTextArea.text += gpx.wpt[0].name + “\n”;

    outputTextArea.text += gpx.children(); works great with the new namespace. Removing the namespace from the gpx makes the code above work great. Any help would be great. Thanks.

  9. sj Says:

    Hi Brian, I’ve touched that code for a year now so not sure if there is an effective workaround or not. Clearly removing the namespace works, so that may be your best bet.

  10. Brian Says:

    Hey SJ, thanks for your reply. I figured after almost a year I wouldn’t get anything!

    The new namespace actually hasn’t worked for me. The only thing that has worked is removing the topografix namespace from the gpx file (what end users want to do that?) or just treating the xml as a string (NOT elegant).

    I’ve been looking into this in my spare time for 3 days now and this is the only place that directly addresses this issue. Do you know if this is an issue with flex or with the namespace?

  11. Brian Says:

    Here’s a sample of parsing the gpx in Flex with the MapQuest ActionScript API – http://devblog.mapquest.com/2009/02/04/dump-your-garmin/

  12. sj Says:

    Hi Brian, I like the source code browser that you’re using. Is it an open source project or did you write it yourself? The MapQuest mash up looks very good. I didn’t know that they were still around.

  13. Brian Says:

    The source code viewer is part of the Export Release Build in FB3.

    Check out the MapQuest Developer Network – definitely still around.

    http://developer.mapquest.com/home

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.