Skip to content Skip to sidebar Skip to footer

How To Export A Gpx File From A Latlng Arraylist

I'm making an application of a real time tracking with geolocation that's why i need to save that track and then export it into a gpx file so users can import it to other applicati

Solution 1:

Ideally a GPX file should consists of valid timestamps which are not available in LatLng class. I would suggest you to use a List of Location class if possible. Following is a sample solution using Location class,

publicstatic void generateGfx(File file, String name, List<Location> points) {

    String header ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?><gpx xmlns=\"http://www.topografix.com/GPX/1/1\" creator=\"MapSource 6.15.5\" version=\"1.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\"><trk>\n";
    name ="<name>"+ name +"</name><trkseg>\n";

    String segments ="";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    for (Location location : points) {
        segments +="<trkpt lat=\""+ location.getLatitude() +"\" lon=\""+ location.getLongitude() +"\"><time>"+ df.format(new Date(location.getTime())) +"</time></trkpt>\n";
    }

    String footer ="</trkseg></trk></gpx>";

    try {
        FileWriter writer = new FileWriter(file, false);
        writer.append(header);
        writer.append(name);
        writer.append(segments);
        writer.append(footer);
        writer.flush();
        writer.close();

    } catch (IOException e) {
        Log.e("generateGfx", "Error Writting Path",e);
    }
}

Solution 2:

Create the necessary tags to make a coherent XML file with the extension .gpx the file should be like the structure of this example:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><gpxxmlns="http://www.topografix.com/GPX/1/1"creator="byHand"version="1.1"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"><wptlat="39.921055008"lon="3.054223107"><ele>12.863281</ele><time>2005-05-16T11:49:06Z</time><name>Cala Sant Vicenç - Mallorca</name><sym>City</sym></wpt></gpx>

Post a Comment for "How To Export A Gpx File From A Latlng Arraylist"