我正在开发一个应用程序来跟踪 gps 坐标并将它们存储到数据库中的单独表中。我想将表格中的 gps 数据导出为 GPX 或 KML 格式。我真的找不到任何教程或解释如何做到这一点。我应该将纬度和经度坐标写入标签之间的文件吗?或者我的文件需要满足什么要求?
我实现的代码:
File directory = Environment.getExternalStorageDirectory();
if (directory.canWrite()){
File kmlFile = new File(directory, "" + table + ".kml");
FileWriter fileWriter = new FileWriter(kmlFile);
BufferedWriter outWriter = new BufferedWriter(fileWriter);
outWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"\n <kml xmlns=\"http://www.opengis.net/kml/2.2\">" +
"\n <Document>" + "\n");
for (int i = 0; i<latArrayList.size();i++){
outWriter.write("<Placemark>" +
"\n <name>" + i + "</name>" +
"\n <description> </description>" +
"\n <Point>" +
"\n <coordinates>" + latArrayList.get(i) + "," + lonArrayList.get(i) + "</coordinates>" +
"\n </Point>" +
"\n </Placemark>" + "\n");
}
outWriter.write("</Document>" +
"\n </kml>");
outWriter.close();
以及我加载到谷歌地图并显示错误坐标的 kml 文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>0</name>
<description> </description>
<Point>
<coordinates>46.09312,18.22501
</coordinates>
</Point>
</Placemark>
<Placemark>
<name>1</name>
<description> </description>
<Point>
<coordinates>46.09317333333333,18.22474833333333
</coordinates>
</Point>
</Placemark>
<Placemark>
<name>2</name>
<description> </description>
<Point>
<coordinates>46.093138333333336,18.22449
</coordinates>
</Point>
</Placemark> </Placemark>
</Document>
</kml>