GPX decoding in Android, with custom class.
Last post describe "Get Latitude and Longitude from gpx file on Android". In order to prepare further expand the function, a custom class, GpxNode, is implemented. It will be enrich in further post.It have the same output as in the last article "Get Latitude and Longitude from gpx file".
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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() +"\n\n";
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.activity_main, 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;
}
}
Next: GPX decoding in Android, with time and ele.
0 komentar:
Posting Komentar