Created
August 2, 2013 20:26
-
-
Save carlosefonseca/6143182 to your computer and use it in GitHub Desktop.
Writes a list of timed coordinates to a GPX file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pt.beware.core; | |
import android.location.Location; | |
import pt.beware.common.utils.Log; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.List; | |
public class GPX { | |
private static final String TAG = GPX.class.getName(); | |
public static void writePath(File file, String n, List<Location> points) { | |
String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?><gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"MapSource 6.15.5\" 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\"><trk>\n"; | |
String name = "<name>" + n + "</name><trkseg>\n"; | |
String segments = ""; | |
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); | |
for (Location l : points) { | |
segments += "<trkpt lat=\"" + l.getLatitude() + "\" lon=\"" + l.getLongitude() + "\"><time>" + df.format(new Date(l.getTime())) + "</time></trkpt>\n"; | |
} | |
String footer = "</trkseg></trk></gpx>"; | |
try { | |
FileWriter writer = new FileWriter(file, false); | |
writer.append(header); | |
writer.append(name); | |
writer.append(segments); | |
writer.append(footer); | |
writer.flush(); | |
writer.close(); | |
if (Config.isDEBUG()) | |
Log.i(TAG, "Saved " + points.size() + " points."); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
Log.e(TAG, "Error Writting Path",e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment