watchPosition constantly firing

923 views
Skip to first unread message

Mark Rupert

unread,
Apr 14, 2015, 10:57:31 PM4/14/15
to phon...@googlegroups.com
I built a test app for watchPosition. For the test I laid the phone next to me and expected that it would not fire until I moved it. However, onSuccess keeps firing every second or two and nothing seems to have changed. The code snippet is:

   navigator.geolocation.getCurrentPosition(onSuccess, onError);
   
var options = { timeout: 30000, timeout:27000, enableHighAccuracy: false };
   watchID
= navigator.geolocation.watchPosition(onSuccess, onError, options);



Kerri Shotts

unread,
Apr 14, 2015, 11:41:56 PM4/14/15
to phon...@googlegroups.com
What kind of phone are you using? What OS?

Mark Rupert

unread,
Apr 15, 2015, 8:51:52 AM4/15/15
to phon...@googlegroups.com
It is a Driod Razr XT907.

mharr

unread,
Apr 15, 2015, 12:20:23 PM4/15/15
to phon...@googlegroups.com
When I was writing a prototype app 5-6 years ago, we found that to be the normal case across all platforms  (iOS, Android, Windows Mobile/Phone, Blackberry) and most devices.  There were a few devices that seemed to be better, but only a few (at this time I don't remember which ones).  We found that we needed to code to only use active notifications when moving above a threshold speed (>15mph, if my memory serves me correctly), and otherwise use timed polled checks. Before we developed this approach, our app would drain a phone battery in just a few hours for all the constant monitoring.

That prototype app died back then (could not get funding for company). I have not had a need to check with current devices, if this is still needed. But I wouldn't be surprised if it was.

Diones A

unread,
Apr 15, 2015, 8:11:27 PM4/15/15
to phon...@googlegroups.com
It was exactly what I was thinking when I developed a phonegap app. Perhaps, it is a misunderstood how watchPosition really works. Anyway, I solved the question using watchPosition just when app is foreground on the assumption that GPS localization have to be constantly updated. When it goes background, I call navigator.geolocation.clearWatch to stop watchPosition interval and start another interval (calling javascript setTimeout or setInterval) to call navigator.geolocation.getCurrentPosition. In this way, I can control how many times GPS become active, controlling battery draining. I don't know if it is the best approach, but it was the only way to workaround the problem about keeping GPS active all the time.

Testing phone: XPERIA S

mharr

unread,
Apr 15, 2015, 8:31:32 PM4/15/15
to phon...@googlegroups.com
It has nothing to do with PhoneGap, it has to do with using GPS radio and forcing it to be open and active.  The prototype apps I referred to were all native code apps, using the (widely-) varied APIs.  In fact, I believe the PhoneGap geolocation plugin for Android, iOS, and Windows Phone are using the respective browsers Html5 Geolocation object, not native GPS APIs.

Rob Willett

unread,
Apr 16, 2015, 7:22:32 AM4/16/15
to phon...@googlegroups.com
I use watchPosition heavily in my code. I've just rebuilt the system with debugging around the watchPosition, tested it again just now and do not get constant updates from the code. Here's my output 

2015-04-16 12:02:21.873 Jambuster[2175:376997] onWatchPositionFailure: Failure {"code":3,"message":"Position retrieval timed out."}

2015-04-16 12:02:21.874 Jambuster[2175:376997] onWatchPositionFailure: Failed

2015-04-16 12:02:37.756 Jambuster[2175:376997] onWatchPositionSuccess: Success {"coords":{"latitude":51.4XXXXXXXXXXX,"longitude":-0.01XXXXXXXXXXXX,"accuracy":65,"altitude":7.154781341552734,"heading":-1,"speed":-1,"altitudeAccuracy":10},"timestamp":"2015-04-16T11:02:37.742Z"}

2015-04-16 12:02:40.760 Jambuster[2175:376997] onWatchPositionFailure: Failure {"code":3,"message":"Position retrieval timed out."}

2015-04-16 12:02:40.761 Jambuster[2175:376997] onWatchPositionFailure: Failed

2015-04-16 12:02:48.665 Jambuster[2175:376997] onWatchPositionSuccess: Success {"coords":{"latitude":51.4XXXXXXXXXXX,"longitude":-0.0XXXXXXXXXXXX,"accuracy":65,"altitude":6.991412162780762,"heading":-1,"speed":-1,"altitudeAccuracy":10},"timestamp":"2015-04-16T11:02:48.652Z"}

2015-04-16 12:02:51.740 Jambuster[2175:376997] onWatchPositionFailure: Failure {"code":3,"message":"Position retrieval timed out."}

2015-04-16 12:02:51.740 Jambuster[2175:376997] onWatchPositionFailure: Failed

2015-04-16 12:02:57.413 Jambuster[2175:376997] onWatchPositionSuccess: Success {"coords":{"latitude":51.4XXXXXXXXXXX,"longitude":-0.0XXXXXXXXXXXX,"accuracy":50,"altitude":8.248470306396484,"heading":null,"speed":0,"altitudeAccuracy":4},"timestamp":"2015-04-16T11:02:57.398Z"}

2015-04-16 12:03:00.599 Jambuster[2175:376997] onWatchPositionFailure: Failure {"code":3,"message":"Position retrieval timed out."}

2015-04-16 12:03:00.600 Jambuster[2175:376997] onWatchPositionFailure: Failed

2015-04-16 12:03:01.279 Jambuster[2175:376997] onWatchPositionSuccess: Success {"coords":{"latitude":51.4XXXXXXXXXXX,"longitude":-0.0XXXXXXXXXXXX,"accuracy":30,"altitude":8.348445892333984,"heading":null,"speed":0,"altitudeAccuracy":4},"timestamp":"2015-04-16T11:03:01.271Z"}

2015-04-16 12:03:04.467 Jambuster[2175:376997] onWatchPositionFailure: Failure {"code":3,"message":"Position retrieval timed out."}

2015-04-16 12:03:04.467 Jambuster[2175:376997] onWatchPositionFailure: Failed


I've blanked out my exact position but I call tell you I live in London, UK. I don;t care about the altitude unless the Thames's floods.


I do get some errors whilst it settles down but I've just looked at the Xcode output and have had no updates, either for positional changes or errors in the last three minutes. So I can say for certain that the constant behaviour updating does not occur on an iPhone 5s running 8.2. I also have a Nexus 5 running Lollipop but I lent that to a friend yesterday and so can't test it on that. I'll try it when I get it back.


Also to comment on other suggestions.


You cannot run timers in the background in iOS, if you use the Xcode simulator it allows you to, Kerri and I had a little discussion on this a few weeks ago, which is where I discovered that the Xcode iOS simulator is more of a "it works a bit like this but its not an exact science". You can run timers in the background there but when you move the code to a iPhone it stops working. 


If you want to run geolocation in the background, the only way I have found on iOS is 


https://github.com/christocracy/cordova-plugin-background-geolocation


This works well but the Android version is very different and you need to stump up for an Android version that works the same way as iOS. We will be buying it as the cost isn't bad and we can't develop it for that price :)


As you suggest, we monitor if we are in the foreground and use one method, once we move to the background we use the background geolocation methods. It was quite easy to implement. However note that the background geolocation codes works differently and that the resolution seems to be more granular as it works on a change in position and that can vary how much is needed.


I would also ask the question of if you really want geolocation to work in the background. We use background GPS recording as we record and learn peoples routes to and from work for our app. Does your use-case really need it? We thought it was essential and then worked out we only needed it for one specific case. Of course YMMV.


We have also found that the watchPosition code as opposed to the content polling of the GPS position, at least on iOS is quite efficient and doesn't seem to drain the battery that much. The screen is the biggest drain and that takes around 50% of the battery as far as we can see. We haven't implemented background GPS positions on Android yet but its on our very near to-do list. 


Hope this helps,


Rob



--
-- 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
---
You received this message because you are subscribed to the Google Groups "phonegap" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phonegap+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jesse Monroy

unread,
Apr 25, 2015, 11:00:01 PM4/25/15
to phon...@googlegroups.com
For those unfamilar with GPS (Global Positioning System) it is good to remember that we are dealing with Satellites, Radio technology and (likely) Atomic clocks.

#1 Satellites do drift, but that is dealt with via the time protocol.
#2 Radio waves are thought to be constant, but infact they vary depending on temperature, solar activity, and time of day.
#3 Atomic clocks are fairly stable and are believe to be synchronized, if they are all set to the same reference. It is presumed they are.

From there it might be good to Google: gps drift in mobile phones

One of the first links shows how even a rather simple and rudimentary test of digital electronics vary widely. And if you consider phone are being pumped out faster than baby kittens, perhaps it all make sense.

GPS Distance Accuracy Test: Smartphone Apps vs. Dedicated GPS

http://www.singletracks.com/blog/gps/gps-distance-accuracy-test-smartphone-apps-vs-dedicated-gps/

Best of Luck,
Jesse
Reply all
Reply to author
Forward
0 new messages