title: Exporting GPS data from a database into GPX or KML in android
body: <p>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?</p>
  
  <p>My implemented code:</p>
  
  <pre><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("&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;" +
                      "\n &lt;kml xmlns=\"http://www.opengis.net/kml/2.2\"&gt;" +
                      "\n &lt;Document&gt;" + "\n");
                      for (int i = 0; i&lt;latArrayList.size();i++){
                          outWriter.write("&lt;Placemark&gt;" + 
                                          "\n &lt;name&gt;" + i + "&lt;/name&gt;" +
                                          "\n &lt;description&gt; &lt;/description&gt;" +
                                          "\n &lt;Point&gt;" +
                                          "\n &lt;coordinates&gt;" + latArrayList.get(i) + "," + lonArrayList.get(i) + "&lt;/coordinates&gt;" +
                                          "\n &lt;/Point&gt;" +
                                          "\n &lt;/Placemark&gt;" + "\n");
                      }
              outWriter.write("&lt;/Document&gt;" + 
                              "\n &lt;/kml&gt;");
              outWriter.close();
  </code></pre>
  
  <p>and a kml file sample which i load into google maps and shows wrong coordinates:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;kml xmlns="http://www.opengis.net/kml/2.2"&gt;
  &lt;Document&gt;
  &lt;Placemark&gt;
  &lt;name&gt;0&lt;/name&gt;
  &lt;description&gt; &lt;/description&gt;
  &lt;Point&gt;
  &lt;coordinates&gt;46.09312,18.22501
  &lt;/coordinates&gt;
  &lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
  &lt;name&gt;1&lt;/name&gt;
  &lt;description&gt; &lt;/description&gt;
  &lt;Point&gt;
  &lt;coordinates&gt;46.09317333333333,18.22474833333333
  &lt;/coordinates&gt;
  &lt;/Point&gt;
  &lt;/Placemark&gt;
  &lt;Placemark&gt;
  &lt;name&gt;2&lt;/name&gt;
  &lt;description&gt; &lt;/description&gt;
  &lt;Point&gt;
  &lt;coordinates&gt;46.093138333333336,18.22449
   &lt;/coordinates&gt;
  &lt;/Point&gt;
  &lt;/Placemark&gt; &lt;/Placemark&gt;
  &lt;/Document&gt;
  &lt;/kml&gt;
  </code></pre>