Titanium Community Questions & Answer Archive

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

GPS Accuracy

Im running into an issue with my GPS accuracy. My code checks for Titanium.Geolocation.getCurrentPosition and if it errors itll spit out an alert asking for the GPS to be turned on. I'm using Android 2.1 and Titanium Mobile SDK 1.4.

This all works, but it seems like its using the network provider to pull the coordinates. I can never get it within a half mile of accuracy.

I'm using both
Titanium.Geolocation.ACCURACY_BEST;
Titanium.Geolocation.PROVIDER_GPS;
yet nothing seems to be solving the issue, I'm not even seeing the GPS icon popping up in my status bar.

Is there a way to pop up an activity indicator while the device gets exact coordinates?

Any help would be greatly appreciated. Here is my current code.
http://pastie.org/1238167

— asked October 21st 2010 by Chris Nelson
  • android
  • geolocation
0 Comments

1 Answer

  • Accepted Answer

    First off, it doesn't look like you are actually setting the preferred provider or accuracy…..you simply are placing them in your code. Try something like:

    Titanium.Geolocation.preferredProvider = Titanium.Geolocation.PROVIDER_GPS;
    Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
    Titanium.Geolocation.distanceFilter = 10;
    

    Also if you read through the documentation/this forum they tend to suggest that calling this method one time will not be sufficient for determining location. They recommend you get the location by continuously listening to the heading event (note this approach will drain battery as GPS doesn't get turned off). You can find an example in the Kitchen Sink.

    Personally, I have moved all of my geolocation code into a function and simply called it twice with setTimeout. Using setTimeout has ensured that it wasn't called back to back….you should wait for one to have the time to finish the call before calling it again, setTimeout accomplish this. Having it called twice seems to provide the accuracy I need and also doesn't require me to continuously listen for the heading event and drain battery.

    The activity indicator is not tied to the GPS. You simply create your own activity indicator & display it, then get your GPS coordinates, then when you are done hide the activity indicator.

    — answered October 22nd 2010 by Mike Robinson
    permalink
    1 Comment
    • Hi Mike,
      I was trying your approach and the kitchen sink. So I came up with the code below?geo.js?.
      So I did a function call from other js file as below. That immediately gave me a runtime error. Please help.

      Ti.include("geo.js");
      
      Titanium.Geolocation.addEventListener('location', locationCallback);
      

      geo.js

      //geo.js
      Titanium.Geolocation.purpose = "Receive User Location";
      Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
      // Set Distance filter. This dictates how often an event fires based on the distance the device moves. This value is in meters.
      Titanium.Geolocation.distanceFilter = 1;
      
      function checkGPSEnability(){
      
          if (Titanium.Geolocation.locationServicesEnabled==false)
          {
              Titanium.UI.createAlertDialog({title:'Kitchen Sink', message:'Your device has geo turned off - turn it on.'}).show();
          }
          else
          {
              if (Titanium.Platform.name != 'android') {
                  var authorization = Titanium.Geolocation.locationServicesAuthorization
                  Ti.API.info('Authorization: '+authorization);
                  if (authorization == Titanium.Geolocation.AUTHORIZATION_DENIED) {
                      Ti.UI.createAlertDialog({
                          title:'No device',
                          message:'You have disallowed Titanium from running geolocation services.'
                      }).show();
                  }
                  else if (authorization == Titanium.Geolocation.AUTHORIZATION_RESTRICTED) {
                      Ti.UI.createAlertDialog({
                          title:'Kitchen Sink',
                          message:'Your system has disallowed Titanium from running geolocation services.'
                      }).show();
                  }
              }
          }
      
      };
      
      function getCurrentLocation(){
          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;
              }
      
              var longitude = e.coords.longitude;
              var latitude = e.coords.latitude;
              var altitude = e.coords.altitude;
              var heading = e.coords.heading;
              var accuracy = e.coords.accuracy;
              var speed = e.coords.speed;
              var timestamp = e.coords.timestamp;
              var altitudeAccuracy = e.coords.altitudeAccuracy;
              Ti.API.info('speed ' + speed);
              //currentLocation.text = 'long:' + longitude + ' lat: ' + latitude;
      
              Titanium.API.info('geo - current location: ' + new Date(timestamp) + ' long ' + longitude + ' lat ' + latitude + ' accuracy ' + accuracy);
              var curr=new [2];
              curr[0]=latitude;
              curr[1]=longitude;
              return curr;  //return the current lat and lon from the api.js function call.
          });
      };
      
          //
          // EVENT LISTENER FOR GEO EVENTS - THIS WILL FIRE REPEATEDLY (BASED ON DISTANCE FILTER)
          //
          var locationCallback = function(e)
          {
              if (!e.success || e.error)
              {
                  return;
              }
      
              var longitude = e.coords.longitude;
              var latitude = e.coords.latitude;
              var altitude = e.coords.altitude;
              var heading = e.coords.heading;
              var accuracy = e.coords.accuracy;
              var speed = e.coords.speed;
              var timestamp = e.coords.timestamp;
              var altitudeAccuracy = e.coords.altitudeAccuracy;
      
              alert('from geo.js lat = ' + latitube + ' lon = ' + longitube);
      
              //Titanium.Geolocation.distanceFilter = 100; //changed after first location event
      
      
              setTimeout(function()
              {
              },100);
      
          };
      

      — commented June 27th 2011 by Mel Maxwell
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.