如何从LatLng ArrayList导出GPX文件

时间:2017-09-29 13:47:40

标签: java android gpx

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

2 个答案:

答案 0 :(得分:0)

创建必要的标签,以制作扩展名为.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>

答案 1 :(得分:0)

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