Вопрос

I'm developing an app which tracks gps coordinates and stores them into seperate tables in a database. I want to export the gps data from the tables into GPX or KML format. I can't really find any tutorials nor explanations how to do this. Should i just write the lat and lon coordinates into a file between tags? Or is there any requirements that my file needs to meet?

My implemented code:

        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();

and a kml file sample which i load into google maps and shows wrong coordinates:

<?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>
Это было полезно?

Решение

Not sure if you still have the problem...

The requirements for a KML file can be found here: https://developers.google.com/kml/documentation/kmlreference

and the xml schema for GPX here: http://www.topografix.com/GPX/1/1/gpx.xsd

I did not 'neg' you, but I guess some people did it as these are pretty easy searches ;)

You are indeed right that you have to write it to a file which you can then publish to Google Earth via an intent.

Good luck

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top