[英]How to export a GPX file from a LatLng ArrayList
我正在制作一个具有地理位置的实时跟踪应用程序,这就是为什么我需要保存该跟踪然后将其导出到gpx文件中以便用户可以将其导入到其他应用程序或进行一些更改的原因,我想知道如何我从LatLng ArrayList制作gpx文件?
创建必要的标记以使扩展名为.gpx
的XML文件变得连贯,该文件应类似于本示例的结构:
<?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>
理想情况下,GPX文件应包含有效的时间戳 ,而这些时间戳在LatLng类中不可用。 如果可能的话,我建议您使用位置列表类。 以下是使用Location类的示例解决方案,
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.