lunedì 2 aprile 2012
Leggere file GPX in Android
Un esempio di come usare la classe java scaricabile qui per leggere i file gpx su Android
package com.gpx;
import android.app.Activity;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
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.util.Log;
class GpxReader
{
private static final SimpleDateFormat gpxDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
public static List<Location> getPoints(File gpxFile)
{
List<Location> points = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
FileInputStream fis = new FileInputStream(gpxFile);
Document dom = builder.parse(fis);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("trkpt");
points = new ArrayList<Location>();
for(int j = 0; j < items.getLength(); j++)
{
Node item = items.item(j);
NamedNodeMap attrs = item.getAttributes();
NodeList props = item.getChildNodes();
Location pt = new Location("test");
pt.setLatitude(Double.parseDouble(attrs.getNamedItem("lat").getTextContent()));
pt.setLongitude(Double.parseDouble(attrs.getNamedItem("lon").getTextContent()));
for(int k = 0; k<props.getLength(); k++)
{
Node item2 = props.item(k);
String name = item2.getNodeName();
if(!name.equalsIgnoreCase("time")) continue;
try
{
pt.setTime((getDateFormatter().parse(item2.getFirstChild().getNodeValue())).getTime());
}
catch(ParseException ex)
{
ex.printStackTrace();
}
}
for(int y = 0; y<props.getLength(); y++)
{
Node item3 = props.item(y);
String name = item3.getNodeName();
if(!name.equalsIgnoreCase("ele")) continue;
pt.setAltitude(Double.parseDouble(item3.getFirstChild().getNodeValue()));
}
points.add(pt);
}
fis.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(ParserConfigurationException ex)
{
}
catch (SAXException ex) {
}
return points;
}
public static SimpleDateFormat getDateFormatter()
{
return (SimpleDateFormat)gpxDate.clone();
}
}
public class GpxActivity extends Activity {
private List<Location> t;
private double lat;
private double lon;
private double alt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File gpx = new File("/mnt/sdcard/malandrino.gpx");
t = GpxReader.getPoints(gpx);
Iterator<Location> it = t.iterator();
while (it.hasNext())
{
Location value = it.next();
lat = value.getLatitude();
lon = value.getLongitude();
alt = value.getAltitude();
Log.d("gpx",Double.toString(lat));
Log.d("gpx",Double.toString(lon));
Log.d("gpx",Double.toString(alt));
}
}
}
Iscriviti a:
Commenti sul post (Atom)
Alpine Linux 2024 su IBM A31
Ho provato a far resuscitare un IBM A31 destinato alla discarica. La macchina ha processore P4, 256 Mb di RAM, la batteria CMOS morta ed e...
-
In questo post viene indicato come creare uno scatterplot dinamico basato da dati ripresi da un file csv (nel dettaglio il file csv e' c...
-
Questo post e' a seguito di quanto gia' visto nella precedente prova Lo scopo e' sempre il solito: creare un sistema che permet...
-
La scheda ESP32-2432S028R monta un Esp Dev Module con uno schermo TFT a driver ILI9341 di 320x240 pixels 16 bit colore.Il sito di riferiment...
Nessun commento:
Posta un commento