how to switch OFF GPS?

You must Login before you can answer or comment on any questions.

hello, i've a problem (on android) switching OFF the phone's GPS. in app.js, i open a single window and start a location listener, which sucessfully turns on the gps (simplifyed code):

var win = Titanium.UI.createWindow({ 
    title: 'my title',
    exitOnClose: true   
});
 
win.addEventListener('close', function(e){
    Titanium.Geolocation.removeEventListener('location', myFunction);
});
 
function myFunction(e){...}; // do something with coordinates here
 
Titanium.Geolocation.addEventListener('location', myFunction);
the app works fine so far, but when i close it with the back-button, the GPS stays active until i deactivate it manually. any idea anybody what's wrong with the code? shouldn't the GPS shut down anyway when i close the app, with or without removing the event listener? thanks for help, uno

— asked 3 years ago by u no
2 Comments
  • I'm using a modified Kitchen Sink GPS example on my Nexus S running 2.3.4:

    https://gist.github.com/1156895

    but my GPS indicator goes off when I exit out of the app. Does this example work for you?

    — commented 3 years ago by Kevin Whinnery

  • i'm sorry for the late reply... your example works, gps turns off when i quit the app. my real life app has a mapview with userLocation set to true. could that make a difference? will try asap. thanks,

    — commented 3 years ago by u no

2 Answers

You must remove the "location" event listener in your window's "blur" event and your window's "close" event. I don't know if that's how Appcelerator intended to require you to do it, but as of SDK 1.7.2 and 1.7.3, that's what you have to do.

— answered 3 years ago by Shawn Lipscomb
answer permalink
3 Comments
  • this is funny: if i remove the location listener in the blur event, GPS turns of for a few seconds and then starts again! well, at least a difference ;)

    — commented 3 years ago by u no

  • The GPS turning off for a few seconds and then starting again is how the GPS works with a Titanium app right now. Even the Kitchen Sink exhibits this behavior. IMO, this is a Titanium bug. In the Kitchen Sink, you can watch as the GPS indicator turns off (for no reason), the real-time location labels turn to and error message that GPS is unavailable. 4 seconds later, the GPS indicator turns back on and you get coordinates again, even though no timers are active.

    Since you are seeing this, I'd say you are not correctly removing the "location" event listener. Please post your code.

    — commented 3 years ago by Shawn Lipscomb

  • More info on the problem of GPS turning off for a few seconds (for no reason) and then starting again: There's an undocumented Ti.Geolocation.frequency property that defaults to 5 (seconds). Setting this to 1 keeps the GPS on (stops it from cycling off and on every few seconds). This is for Android...I'm not sure if the same problem exists for iPhone or not.

    — commented 2 years ago by Shawn Lipscomb

it (again) turns out for me that it has to do with how i open the window. if i rewrite the above example into the following: app.js:

var win = Titanium.UI.createWindow({ 
    title: 'my title',
    url: 'main.js',
    exitOnClose: true   
});
win.open({fullscreen:true, navBarHidden: false});
// the following affects a mapview, not clear yet how & why
// win.open({fullscreen:true, navBarHidden: false, modal:true});
and put the code in main.js:
var win = Titanium.UI.currentWindow;
function myFunction(e){...}; // do something with coordinates here
Titanium.Geolocation.addEventListener('location', myFunction);
...
then it works as expected. e.g. the GPS shuts down after closing the app, and stays off. all without explicitely removing the location listener. however, since my real life apps needs a mapview, i have to experiment with the different window properties. mapviews seem to have problems with these heavyweight windows on android. modal:true seems to be the key. (discussed also here). will try... thanks,

Your Answer

Think you can help? Login to answer this question!