Jan 17, 2013

Get Latitude and Longitude from gpx file

GPX, or GPS eXchange Format is an XML schema designed as a common GPS data format for software applications.

It can be used to describe waypoints, tracks, and routes. The format is open and can be used without the need to pay license fees. Its tags store location, elevation, and time and can in this way be used to interchange data between GPS devices and software packages. Such computer programs allow users, for example, to view their tracks, project their tracks on satellite images or other maps, annotate maps, and tag photographs with the geolocation in the Exif metadata.

~ source: Wikipedia - GPS eXchange Format.

This example demonstrate how to parse gpx file, to generate a list of Location.

Get Latitude and Longitude from gpx file

Create a text file "test.gpx", copy and modify from the example in Wikipedia - GPS eXchange Format, save in temp folder of Android device's sdcard.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" creator="Oregon 400t" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
    <link href="http://www.garmin.com">
      <text>Garmin International</text>
    <name>Example GPX Document</name>
      <trkpt lat="47.644548" lon="-122.326897">
      <trkpt lat="47.644001" lon="-122.326001">
      <trkpt lat="47.644002" lon="-122.326002">

package com.example.androidcodinggpx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

 protected void onCreate(Bundle savedInstanceState) {
  String path = Environment.getExternalStorageDirectory().toString() + "/temp/test.gpx";
  TextView textInfo = (TextView)findViewById(R.id.info);
  String info = "";
  File gpxFile = new File(path);
  info += gpxFile.getPath() +"\n\n";

  List<Location> gpxList = decodeGPX(gpxFile);

  for(int i = 0; i < gpxList.size(); i++){
   info += ((Location)gpxList.get(i)).getLatitude() 
     + " : " 
     + ((Location)gpxList.get(i)).getLongitude() + "\n";

 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 private List<Location> decodeGPX(File file){
  List<Location> list = new ArrayList<Location>();

  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  try {
   DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
   FileInputStream fileInputStream = new FileInputStream(file);
   Document document = documentBuilder.parse(fileInputStream);
   Element elementRoot = document.getDocumentElement();
   NodeList nodelist_trkpt = elementRoot.getElementsByTagName("trkpt");

   for(int i = 0; i < nodelist_trkpt.getLength(); i++){
    Node node = nodelist_trkpt.item(i);
    NamedNodeMap attributes = node.getAttributes();
    String newLatitude = attributes.getNamedItem("lat").getTextContent();
    Double newLatitude_double = Double.parseDouble(newLatitude);
    String newLongitude = attributes.getNamedItem("lon").getTextContent();
    Double newLongitude_double = Double.parseDouble(newLongitude);
    String newLocationName = newLatitude + ":" + newLongitude;
    Location newLocation = new Location(newLocationName);

  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
  } catch (SAXException e) {
   // TODO Auto-generated catch block
  } catch (IOException e) {
   // TODO Auto-generated catch block
  return list;


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >

        android:text="@string/hello_world" />


Next: GPX decoding in Android, with custom class.

1 comment:

  1. Dear sirs,
    Thanks for your useful example! This works great!
    Additionally, can you explain how to extract also the ele value from each trkpt?
    I tried several ways but with no success :(

    Thanks in advance!


Infolinks In Text Ads