DEV Community 👩‍💻👨‍💻

Discussion on: You CAN Convert your Callbacks to Promises

prof3ssorst3v3 profile image
Steve Griffith Author

Thanks for the comments and questions. :)
I have a video tutorial on my YouTube channel all about Geolocation - . There are other geolocation tutorials with Google maps, Cordova, and React Native too, but this is the first one focused on just geolocation.
Any time you are going to use any API you want to put the check for the feature as close as possible to the actual use as you can. You never know where your function module may end up. So, you should keep the code all bundled together so it can be moved together.
If you want to avoid the check, which really is only taking a fraction of a millisecond to check for the existence of a property inside an object, then you could create a variable outside the function, still inside the module, to save the result. But then you need to check the value of that variable. Either check would take the same amount of time but the second way requires using extra memory to save the value.

rowild profile image
Robert Wildling

Thank you for your answer! I need to explain my situation a bit more. First of all, I am talking about the "watchGeolocation" option, not the "getCurrentPosition" option that you use in your other tutorials. (BTW: I know that there are many tutorials out there, but it seems none talks about the proper usage of "maximumAge".)
In my understanding, when I set maximumAge to e.g. 5000, then all incoming GPS signals within the next 5 seconds should have the same timestamp, since they would be fetched from the cache. Right? But this is not the case.

I setup an example here:
The repo is here:
(Sorry for the meaningless git messages...)

This example shows clearly that the timestamp changes whenever a GPS signal is recognised (about every second).

So I wonder: What the heck is this maximumAge value doing really?

The idea is to build a tracking app. The user should set a maximumAge (a higher one, when the activity is walking, a lower one, when the activity is biking etc.. the usual stuff...), so the app does not save each and every incoming GPS value. I would set the first new value to a var called "cachedTimestamp" and compare all the next incoming values with that cached timestamp. That would enable me to save a new value only every 30 seconds (e.g.).
But since the timestamp changes all the time, this is not the way to go.

Any idea what is going n here?

Thank you very much!

Thread Thread
prof3ssorst3v3 profile image
Steve Griffith Author • Edited on

maximumAge doesn't keep the same timestamp with each request.
The browser will cache a coordinates object with a timestamp.
Each subsequent time you make a request, it will look at the current timestamp and compare it with the cache result's timestamp. If you have not reached the maximumAge yet then you get a copy of the coordinates object along with a current timestamp.
The position object
timestamp: current value for the current request
coords: { this could be the cached coords object or a new one if you are past the cached timestamp }
The browser is allowed to adjust the result in the interest of better battery life and lowered use of the radio antenna in the device.
It also uses the highAccuracy setting to decide what to do based on other things happening on the device. When you say that you want highAccuracy, you are really just saying that it is allowed for the device to use it, not that the device MUST use it.
The developer provides their best intention to the browser through the options object, but it is still up to the browser to interpret those settings.
Many of the HTML5 APIs work this way.

Thread Thread
rowild profile image
Robert Wildling

Thank you again very much, Steve!

What I observe is that the coordinate values change all the time, no matter what. So if I understand you correctly: Using maximumAge to manage the amount of geolocation points to be saved to a data store is unreliable. If I want to save a geolocation datum only every 3rd second; i need to implement my own timer.
To be honest: I do not really understand, what this setting is good for, if the browser does whatever it wants whenever it wants... :-(

However, I love your tutorials and texts and am looking forward to new stuff of yours! Thank you again! Have a good day!

Thread Thread
prof3ssorst3v3 profile image
Steve Griffith Author

You're very welcome.
Just bear in mind that changing the maximumAge option, the enableHighAccuracy option, or using a timer you create yourself will get similar results. The management of the radio in the phone is beyond absolute control of the JS. It will always be the device and the browser that get to make the final decision about how to use the radio - wifi, gps, etc.