cordova-plugin-geolocation: Timestamp of getCurrentPosition wrong? (iOS 12)
Bug Report
Problem
When comparing the timestamps of the object returned by the geolocation plugin, we found that the timestamps are from before the call to the getCurrentPosition function. We log
-
the time when we make the request to getCurrentPosition
-
the timestamp returned by the geolocation.
What is expected to happen?
The timestamp of the geolocation-object should be newer than the timestamp that we log before the call to the plugin is made.
What does actually happen?
The timestamp of the geolocation-object is most of the times (not consistently) older than the timestamp that we log before the call to the plugin is made. Example: Timestamp_Call - Timestamp_GPS 15:45:28.475 - 15:45:28.466
The question is: Is the timestamp wrong or does the plugin return a value coming from an older call to getCurrentPosition?
Information
Our options are: timeout: Infinity, maximumAge: 0, enableHighAccuracy: true
I checked this on a trivial app containing no other plugins. In the browser we observe the expected behaviour with the timestamp of the GPS position being slightly newer than the timestamp of the call. Example: Timestamp_Call - Timestamp_GPS 15:45:0.241 - 15:45:0.276
I deployed it via Xcode and ran it directly on the device.
Command or Code
App component:
import { Component, ViewChild } from '@angular/core';
import { Nav } from 'ionic-angular';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
private _timer;
private readStartTimestamp;
private log = [];
ngOnInit() {
this._startWatchingPosition();
}
_getCurrentPosition() {
this.readStartTimestamp = new Date();
navigator.geolocation.getCurrentPosition((position) => {
(<any>position).gpsStartTimestamp = this.formatDate(this.readStartTimestamp);
(<any>position).gpsTimestamp = this.formatDate(new Date(position.timestamp));
this.log.push(position);
}, (err) => {
console.log("Error getting current location: " + err.message);
}, {
timeout: Infinity,
maximumAge: 0,
enableHighAccuracy: true
});
}
_startWatchingPosition() {
clearInterval(this._timer);
this._getCurrentPosition();
this._timer = setInterval(() => {
this._getCurrentPosition();
}, 5000);
console.log("Watching position started");
};
private formatDate(date: Date) {
return date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + '.' + date.getMilliseconds();
}
}
App template:
<ion-content>
<h1>TESTAPP Geolocation Plugin</h1>
<div style="padding: 10px;overflow: scroll; -webkit-overflow-scrolling: touch; user-select: text; -webkit-user-select: text" id="text">
<ion-item *ngFor="let position of log.slice().reverse()">
{{position.gpsStartTimestamp}} - {{position.gpsTimestamp}} - {{position.coords.longitude}} / {{position.coords.latitude}}
</ion-item>
</div>
</ion-content>
If necessary, I can provide a sample project, but it really only consists of these two files + bootstrapping.
cordova add platform ios cordova prepare ios Run with Xcode on iOS Device
Environment, Platform, Device
I tested it on iOS 12.1 (iPhone) and 12.3 (iPad), both showing the mentioned behaviour. I didn’t test it on Android. The browser does not show the mentioned behaviour, but behaves as expected.
Version information
cordova platform ls
Installed platforms:
ios 5.0.1
cordova plugin ls
cordova-plugin-geolocation 4.0.1 “Geolocation”
cordova --version
9.0.0 (cordova-lib@9.0.1)
from package.json:
"cordova-ios": "5.0.1",
"cordova-plugin-geolocation": "4.0.1",
"ionic-angular": "3.9.6"
ionic --version
4.10.2
OSX 10.14.5 Xcode 10.2.1
Checklist
- I searched for existing GitHub issues
- I updated all Cordova tooling to most recent version
- I included all the necessary information above
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (8 by maintainers)
Here you go: https://github.com/ninaDeimos/geolocationTesting
I had a quick look at the iOS CLLocationManager documentation and saw no way to control the frequency of updates… but I did found something interesting.
Apple recommends using the
requestLocation
method for obtaining a single update. CordovagetCurrentPosition
does not use that method and instead usesstartUpdatingLocation
, which is a method used to watch for GPS continuously.I’m not sure if there is any particular reason why it’s done that way but I do think it’s worth checking out to see if using
requestLocation
forgetCurrentPosition
on the native side changes behaviour. I unfortunately don’t have easy access to a mac so I can’t really experiment this myself.If you’re not comfortable looking at or changing the native code yourself, then I would suggest sharing a reproduction repo that can be used as a testing ground.