2012-03-22 2 views
1

전화 (geoLocation 데이터)에서 데이터를 가져와 JSON 객체에 넣고 네트워크를 통해 데이터베이스에 저장하여 저장하려고합니다. 나는이 객체를 만드는 방법과 객체의 배열이어야하는지 확신 할 수 없다. 또한 서버로 보내도록 업데이트 할 때 새 정보를 수신하도록 객체를 설정하는 방법은 무엇입니까? 현재 가지고있는 코드는 다음과 같습니다. 나는 이클립스, phonegap, 자바 스크립트와 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

관련 문제