Pages

Thursday, August 22, 2013

Export coordinates obtained from Location Providers to GPX file.

This code will help you convert the coordinates and data that you obtained from Location Providers(Both GPS and Network) to GPX file.

First of all you need to add on your coordinates and data to listarray and then pass on these values to the Filewriter.



Lets say these are your listarrays:



   latlist.add(latitudes)  
   lonlist.add(longitudes)  
   timelist.add(time);  
   altitudelist.add(altitude);  
   speedlist.add(speed);  


Then pass on these values to Writer as given below:



 File root = new File(Environment.getExternalStorageDirectory().getPath() + "/MyGPX/");  
               root.mkdirs();  
              if (root.canWrite()) {  
          File gpxfile = new File(root, "Mytrace.gpx");  
          FileWriter gpxwriter = new FileWriter(gpxfile);  
          BufferedWriter out = new BufferedWriter(gpxwriter);  
  out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>"  
              + "\n\n" + "<gpx version=\"1.0\" creator=\"Traceify\">"  
               + "\n" + "<trk>" + "\n");  
          out.write("<name>MyGPX GPX DOC</name>"+"\n");  
          out.write("<trkseg>"+"\n");  
          Log.e("list size is","PPPP"+latlist.size() );  
          for (int i = 0; i < latlist.size(); i++) {  
          // String outDate = ISO_8601_DATE_TIME.format(timelist.get(i));  
           Log.e("writing to file","PPPPPPPPP" );  
 out.write("<trkpt" +" " +"lat="+"\""+(latlist.get(i))+"\""+" "+"lon="+"\""+(lonlist.get(i))+"\"" +">" + "\n"  
      + "<ele>"+(altitudelist.get(i))+"</ele>" + "\n" + "<speed>"+(speedlist.get(i))+"</speed>" + "\n"  
             +"<time>"+(timelist.get(i))+"</time>" + "\n"+ "</trkpt>" +"\n");  
          }  
          out.write("</trkseg>" + "\n" + "</trk>" +"\n"+"</gpx>");  
          out.close();  
          latlist.clear();  
          lonlist.clear();  
          Log.e("cleared from list","dffffffffffff" );  
          timelist.clear();  
          altitudelist.clear();  
          speedlist.clear();  
          try{  
          cur.close();  
         db.close();  
          }catch (Exception e) {  
    // TODO: handle exception  
    }  



You may customize the code depending on your needs!!
Thanks...:)