我正在制作一个带有地理位置的实时跟踪应用程序,这就是为什么我需要保存该轨迹,然后将其导出到 gpx 文件中,以便用户可以将其导入到其他应用程序或进行一些更改,我想知道什么我如何从 LatLng ArrayList 制作 gpx 文件?
最佳答案
最好是 GPX 文件 should consists of valid timestamps LatLng 中不可用类(class)。我建议您使用 Location 列表如果可以的话上课。以下是使用 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);
}
}
关于java - 如何从 LatLng ArrayList 导出 GPX 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46490192/