Turn off Geolocation Services on pause on iPhone

1,428 views
Skip to first unread message

MIPs

unread,
Jun 16, 2011, 5:03:06 PM6/16/11
to phon...@googlegroups.com
I have an app that is using geolocation services (PhoneGap 0.9.5.1 on iOs 4.3).

It is designed to run in background as it tracks the user.  So I have used "App registers for location updates".  That works fine and even if the user runs another app, mine keeps logging their location as designed.

When the app starts it has to immediately start using Geolocation to enable the GPS and get an accuracy reading.  The user then starts the tracking when they are ready.  When they have finished, they can stop tracking, but the app needs to continue to access Geolocation services in case they start again and to maintain the GPS lock.

But of course the battery usage is terrible.  That is fine when the user wants the app to track their location, but once they have completely finished how do I turn off Geolocation when the app moves into background? 

I have intercepted the "pause" event and at that point clear the interval function  that is calling navigator.geolocation.getCurrentPosition() if they are not tracking at the time, renabling on "resume", but the icon showing Geolocation services are active remains and battery life would suggest that the service is using power.

At the moment the only way to reduce to stop the power usage is to terminate the program - but many user would not know how to do that or that they need to and I suspect Apple will reject the app on those grounds.

Any suggestions would be appreciated.



MIPs

unread,
Jun 16, 2011, 5:32:27 PM6/16/11
to phon...@googlegroups.com
Found the solution!

Have added "navigator.geolocation.stop();" to my pause event.

Always simple when you know how.

Small point to PhoneGap team.  navigator.geolocation.stop() and start() are not in the API documentation! I found it walking through the PhoneGapLib.js.

Shazron Abdullah

unread,
Jun 16, 2011, 5:50:05 PM6/16/11
to phon...@googlegroups.com
Thanks Nick,
The reason stop and start are not in the documentation is because it's iOS only and not part of the W3C Geolocation API. We'll have to re-visit the implementation with the spec.

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

Reply all
Reply to author
Forward
0 new messages