Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Geolocation Returning Null on iOS Device

I'm trying my app to retrieve the user's current location in my iOS app.

This then passes the longitude and latitude into a web service which retrieves data from my server.

However, on the device it just returns null data meaning it'll bring me back incorrect information.

Can anyone see what I might be doing wrong with the code below?

Code on pastebin

Hoping it's a easy fix! It's driving me crazy!

Many thanks

Simon

— asked February 12th 2012 by Simon Hume
  • geolocation
  • ios
  • json
0 Comments

2 Answers

  • Try putting the call to loadgigs() in Titanium.Geolocation.getCurrentPosition(function(e) so that the function doesn't run before you get the coordinates. I don't really see why the data should be returned null from the server

    — answered February 12th 2012 by Cody Hartman
    permalink
    2 Comments
    • I tried that, and it'll then just return nothing at all in the TableView

      — commented February 12th 2012 by Simon Hume
    • Well try using gigs.results[i].yourvariable

      — commented February 12th 2012 by Cody Hartman
  • just replace your code with the below one and comment the line where you call the loadgigs() method.

    Titanium.Geolocation.getCurrentPosition(function(e)
            {
                    if (!e.success || e.error)
                    {
                            currentLocation.text = 'error: ' + JSON.stringify(e.error);
                            Ti.API.info("Code translation: "+translateErrorCode(e.code));
                            alert('error ' + JSON.stringify(e.error));
                            return;
                    }
    
                    longitude = e.coords.longitude;
                    latitude = e.coords.latitude;
                    altitude = e.coords.altitude;
                    heading = e.coords.heading;
                    accuracy = e.coords.accuracy;
                    speed = e.coords.speed;
                    timestamp = e.coords.timestamp;
                    altitudeAccuracy = e.coords.altitudeAccuracy;
    
            loadgigs();
    
            });
    
    — answered February 13th 2012 by Mitul Bhalia
    permalink
    7 Comments
    • Great, i'll give it a try when I get back to my machine

      — commented February 13th 2012 by Simon Hume
    • if you satisfy with the answer you can mark this answer as accepted

      — commented February 13th 2012 by Mitul Bhalia
    • Just a quick question (as I can't try this out till a bit later).

      Will adding loadgigs(); at the end of the Geo function have trouble referencing the function?

      The loadgigs function gets declared directly below the geo function, is this OK?

      — commented February 13th 2012 by Simon Hume
    • Running it through jslint.com it says

      'loadGigs' was used before it was defined.

      — commented February 13th 2012 by Simon Hume
    • yes this is ok. it does not give any error but if you want to remove that warning then you can put that function after declaration of variables

      — commented February 13th 2012 by Mitul Bhalia
    • I've just changed the code and it's still returning blank values i'm afraid.

      Location services are turned on, I've moved the loadGigs() into the geo function in your description above, and commented it out of the bottom of my script.

      I just get results that are closest to 0,0 rather than my true location.

      Any other thoughts?

      — commented February 13th 2012 by Simon Hume
    • Titanium.Geolocation.getCurrentPosition(function(e)
             if (!e.success || e.error)
                      {
                              currentLocation.text = 'error: ' + JSON.stringify(e.error);
                              Ti.API.info("Code translation: "+translateErrorCode(e.code));
                              alert('error ' + JSON.stringify(e.error));
                              return;
                      }
      
                      longitude = e.coords.longitude;
                      latitude = e.coords.latitude;
                      altitude = e.coords.altitude;
                      heading = e.coords.heading;
                      accuracy = e.coords.accuracy;
                      speed = e.coords.speed;
                      timestamp = e.coords.timestamp;
                      altitudeAccuracy = e.coords.altitudeAccuracy;
      
              Ti.App.Properties.setString("longitude",longitude);
              Ti.App.Properties.setString("latitude",latitude);
      
              loadgigs();
          });
      

      you are testing on iphone device..?

      one thing you can do is that store your location in properties in app.js file and then get that location in this window and then call your function.

      — commented February 14th 2012 by Mitul Bhalia
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.