I am wanting to get data from a phone (geoLocation data) and put that into a JSON Object and send that over a network to a database to be stored. I am unsure how to create this object and if it should be an array of objects. Also, how do I setup the object to receive new information as it updates to send to the server? Here is the code I currently have. I am using eclipse, phonegap, javascript and JSON.
var JSONID = 0;
// Create a JSON Object Array
geoLocJSON ();
/* This is what I want my Object to look like.
[
{
"id" : 0,
"geoLoc" : {
"Lat" : "",
"Long" : ""
},
"direction" : {
"Alt" : "",
"Head" : "",
"Speed" : ""
},
"Time" : ""
};
]
*/
// onSuccess Geolocation
function onSuccess(position){
// Testing for data
var element = document.getElementById('geolocation');
element.innerHTML =
'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + new Date(position.timestamp) + '<br />' +
'<hr />' + element.innerHTML;
// Puts into JSON Object
geoLocJSON.id = JSONID;
geoLocJSON.geoLoc.Lat = position.coords.latitude;
geoLocJSON.geoLoc.Long = position.coords.longitude;
geoLocJSON.direction.Alt = position.coords.altitude;
geoLocJSON.direction.Head = position.coords.heading;
geoLocJSON.direction.Speed = position.coords.speed;
geoLocJSON.Time = new Date(position.timestamp);
// Increments the JSONID
JSONID++;
}
This will then be posted to the server after 1 minute of collecting data and the JSON Object will be erased UNLESS the POST is unsuccessful, then the object will be stored locally to the device and posted later when a network is available again.
Thank you for your help.