Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a simple javascript object with some device information including last known geolocation (longitude and latitude only). I'm using JSON.stringify to turn this object in to JSON, but the one variable I have that isn't boolean/text/number gets ignored.

exports.CallContext = function() {
var deviceId;
var applicationId;
var userLocation = {lat:0, lon:0};
var operatingSystem;

this.create = function(b) {     
    this.deviceId = Titanium.Platform.id;
    this.applicationId = Alloy.Globals.appId;
    Titanium.Geolocation.getCurrentPosition(function(e) {
        if(e.error) {
            Ti.API.info(e.error);
        } else {
            this.userLocation = {lat:e.coords.latitude, lon:e.coords.longitude};
        }
    });
    this.operatingSystem = Titanium.Platform.version;
};
};

After doing JSON.stringify(myCallContext); I get a JSON object with deviceId, applicationId and operatingSystem (etc., some omitted), not sure why it isn't including userLocation. Tried turning it in to an array, didn't work still, and still doesn't include it when I get rid of the getCurrentLocation and leave it with dummy info.

share|improve this question
    
did you get anything by logging this.userLocation? – Dragon Mar 25 '15 at 12:16

You have to implement toJSON() function inside nested objects to be able to serialise them to JSON format correctly

More info here

share|improve this answer
    
Sorry no not it! One of my first thoughts too. I think my geolocation request was not working at all. – Liam MacDonald Mar 26 '15 at 9:52

you need to define userLocation as object example :

  var userLocation =[];

and then set lat & lon as items in object ex->

 userLocation [0]=0 ;//as lat
    userLocation [1]=0;//as lon
share|improve this answer
    
I already am doing that. I think the problem is to do with the Titanium.Geolocation.getCurrentPosition call as when I get rid of that and replace it with just this.userLocation = {lon:fake, lat:fake}; it does get serialized. – Liam MacDonald Mar 25 '15 at 11:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.