将数据从数据库导出到android中的GPX或KML

时间:2013-04-24 08:13:41

标签: android gps kml gpx

我正在开发一个跟踪gps坐标的应用程序,并将它们存储到数据库中的单独表中。我想将表中的gps数据导出为GPX或KML格式。我真的找不到任何教程或解释如何做到这一点。我应该将lat和lon坐标写入标签之间的文件中吗?或者我的文件需要满足哪些要求?

我的实施代码:

        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>

1 个答案:

答案 0 :(得分:0)

不确定您是否还有问题...

可在此处找到KML文件的要求: https://developers.google.com/kml/documentation/kmlreference

和GPX的xml架构: http://www.topografix.com/GPX/1/1/gpx.xsd

我没有“否定”你,但我猜有些人这样做,因为这些搜索非常简单;)

您确实必须将其写入文件,然后您可以通过意图将其发布到Google地球。

祝你好运