Decoding GPX File Using A Custom Class

This example shows how you can get location by decoding GPX code in a custom class.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it GPXDecodingWithCustomClass.

2.) Write following into main.xml:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".GPXDecodingWithCustomClass" >

<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</LinearLayout>

 

 

3.) Write following permissions into your manifest file:

 

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

 

4.) Create a text file “test.gpx” and write following into it. Save in temp folder of Android device’s sdcard’s /sdcard/temp/test.gpx

 

<?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">
<metadata>
<link href="http://www.garmin.com">
<text>Garmin International</text>
</link>
<time>2009-10-17T22:58:43Z</time>
</metadata>
<trk>
<name>Example GPX Document</name>
<trkseg>
<trkpt lat="47.644548" lon="-122.326897">
<ele>4.46</ele>
<time>2009-10-17T18:37:26Z</time>
</trkpt>
<trkpt lat="47.644001" lon="-122.326001">
<ele>4.94</ele>
<time>2009-10-17T18:37:31Z</time>
</trkpt>
<trkpt lat="47.644002" lon="-122.326002">
<ele>6.87</ele>
<time>2009-10-17T18:37:34Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>

 

5.) Run for output.

Steps:

1.) Create a project named GPXDecodingWithCustomClass and set the information as stated in the image.

Build Target: Android 4.4
Application Name: GPXDecodingWithCustomClass
Package Name: com.example.GPXDecodingWithCustomClass
Activity Name: GPXDecodingWithCustomClass

gpxdecoding1

2.) Open GPXDecodingWithCustomClass.java file and write following code there:

 

package com.example.gpxdecodingwithcustomclass;

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 GPXDecodingWithCustomClass extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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() +"nn";

List<GpxNode> gpxList = decodeGPX(gpxFile);

for(int i = 0; i < gpxList.size(); i++){
info += gpxList.get(i).getLocationString() + "n";
}

textInfo.setText(info);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.gpxdecoding_with_custom_class, menu);
return true;
}

class GpxNode{

Location location;
String ele;
String time;

GpxNode(){
location = null;
ele = "";
time = "";
}

GpxNode(Location loc){
location = loc;
ele = "";
time = "";
}

GpxNode(Location loc, String e, String t){
location = loc;
ele = e;
time = t;
}

void setEle(String e){
ele = e;
}

void setTime(String t){
time = t;
}

Location getLocation(){
return location;
}

String getLocationString(){
return location.getLatitude() + ":" + location.getLongitude();
}

}

private List<GpxNode> decodeGPX(File file){
List<GpxNode> list = new ArrayList<GpxNode>();

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);
newLocation.setLatitude(newLatitude_double);
newLocation.setLongitude(newLongitude_double);

GpxNode newGpxNode = new GpxNode(newLocation);
list.add(newGpxNode);

}

fileInputStream.close();

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

return list;
}
}

 

 

3.) Compile and build the project.

Output

gpxdecoding2