2012-03-22 126 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