我正在制作一个具有地理位置的实时跟踪应用程序,这就是为什么我需要保存该跟踪然后将其导出到gpx文件中以便用户可以将其导入到其他应用程序或进行一些更改的原因,我想知道如何我从LatLng ArrayList制作gpx文件?

最佳答案

理想情况下,GPX文件should consists of valid timestampsLatLng类中不可用。如果可能的话,我建议您使用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);
    }
}

09-27 00:05