问题
I'm making an application of a real time tracking with geolocation that's why i need to save that track and then export it into a gpx file so users can import it to other application or making some changes, what i want to know is how can i make a gpx file from a LatLng ArrayList?
回答1:
Create the necessary tags to make a coherent XML file with the extension .gpx
the file should be like the structure of this example:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="byHand" 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">
<wpt lat="39.921055008" lon="3.054223107">
<ele>12.863281</ele>
<time>2005-05-16T11:49:06Z</time>
<name>Cala Sant Vicenç - Mallorca</name>
<sym>City</sym>
</wpt>
</gpx>
回答2:
Ideally a GPX file should consists of valid timestamps which are not available in LatLng class. I would suggest you to use a List of Location class if possible. Following is a sample solution using Location class,
public static void generateGfx(File file, String name, 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";
name = "<name>" + name + "</name><trkseg>\n";
String segments = "";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
for (Location location : points) {
segments += "<trkpt lat=\"" + location.getLatitude() + "\" lon=\"" + location.getLongitude() + "\"><time>" + df.format(new Date(location.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();
} catch (IOException e) {
Log.e("generateGfx", "Error Writting Path",e);
}
}
来源:https://stackoverflow.com/questions/46490192/how-to-export-a-gpx-file-from-a-latlng-arraylist