Saving lat longs from database to GPX file.Android
-
02-06-2021 - |
Question
I have created sqlite database which stores Latitude,Longitude and some other information like description.
Now I want to extract this information and write into a GPX file to my SD card for further analysis purpose. Anyone have an idea about how to do this?
Any helpful links, sources etc.?
Solution
Thanks friends. Above two links help me to solve my problem, Here is the code for writing database values to .kml file
try {
File root = Environment
.getExternalStorageDirectory();
if (root.canWrite()) {
File gpxfile = new File(root, ""
+ value + ".kml");
FileWriter gpxwriter = new FileWriter(
gpxfile);
BufferedWriter out = new BufferedWriter(
gpxwriter);
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "\n"
+ "<kml xmlns=\"http://www.opengis.net/kml/2.2\">"
+ "\n" + "<Folder>" + "\n");
for (int i = 0; i < latarray.length; i++) {
out.write("<Placemark>" + "\n"
+ "<name> Point "
+ i
+ "</name>"
+ "\n"
+ "<description>"
+ ""
+ discription[i]
+ "</description>"
+ "\n"
+ "<Point>"
+ "\n"
+ "<coordinates>"
+ ""
+ (lonarray[i] / (1E6))
+ ","
+ ""
+ (latarray[i] / (1E6))
+ ","
+ ""
+ accuarray[i]
+ "</coordinates>"
+ "\n"
+ " </Point>"
+ "\n"
+ "</Placemark>" + "\n");
}
out.write("</Folder>" + "\n" + "</kml>");
out.close();
mydb.close();
}
} catch (IOException e) {
Log.e("Cant write", "Could not write file "
+ e.getMessage());
}
and here is the example to read value from database
public int[] lattodraw() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_Latitude,
KEY_Longitude, KEY_Accuracy, KEY_ROWID, KEY_Latitude,
KEY_Longitude, KEY_Accuracy, KEY_Discription, KEY_North,
KEY_East, KEY_South, KEY_West };
Cursor locationCursor = ourDatabase.query(DATABASE_TABLE, columns,
null, null, null, null, null);
locationCursor.moveToFirst();
int count = locationCursor.getCount();
int latarray[] = new int[count];
int i = 0;
do {
int latitude = (int) (locationCursor.getDouble(locationCursor
.getColumnIndex(KEY_Latitude)) * 1E6);
latarray[i] = latitude;
i++;
} while (locationCursor.moveToNext());
locationCursor.close();
return latarray;
}
OTHER TIPS
The best solution I found so far for both writing and reading GPX is this one: http://sourceforge.net/projects/gpxparser/
here it is:
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File gpxfile = new File(root, "gpxfile.gpx");
FileWriter gpxwriter = new FileWriter(gpxfile);
BufferedWriter out = new BufferedWriter(gpxwriter);
out.write("Hello world");
out.close();
}
} catch (IOException e) {
Log.e(TAG, "Could not write file " + e.getMessage());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow