2012-03-22 127 views
1

我想从手机获取数据(地理位置数据)并将其放入JSON对象中,并通过网络将其发送到要存储的数据库。我不确定如何创建这个对象,如果它应该是一个对象数组。另外,如何设置对象以便在更新后发送到服务器时接收新信息?这是我现在的代码。我使用的是eclipse,phonegap,javascript和JSON。如何创建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++; 
    } 

这一操作将在1分钟后收集数据的被张贴到服务器和JSON对象将被删除,除非在POST不成功,则该对象将被本地存储到设备,并张贴后,当一个网络再次可用。

谢谢你的帮助。

回答

0
var JSONID = 0; 


// Create a JSON Object Array 
var geoLocJSON = new Array(); 

/* 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; 

    var myJSON = { 
     "id":JSONID, 
     "geoLoc":{ 
      "Lat":position.coords.latitude, 
      "Long":position.coords.longitude 
     }, 
     "direction":{ 
      "Alt":position.coords.altitude, 
      "Head":position.coords.heading, 
      "Speed":position.coords.speed 
     }, 
     "Time":new Date(position.timestamp 
    }; 

    // Increments the JSONID 
    JSONID++; 
    geoLocJSON.push(myJSON); 
} 
+1

工作就像一个魅力,谢谢! – jdelbs18 2012-11-12 18:18:55