nativescript-geolocation icon indicating copy to clipboard operation
nativescript-geolocation copied to clipboard

iOS getCurrentLocation returns nothing after a few seconds

Open RaisinBread42 opened this issue 6 years ago • 9 comments

Please, provide the following version numbers that your issue occurs with:

"nativescript": {
    "id": "org.nativescript.application",
    "tns-android": {
      "version": "5.0.0-rc-2018-10-18-172901-01"
    },
    "tns-ios": {
      "version": "5.0.0"
    }
  },
  "dependencies": {
    "http": "0.0.0",
    "https": "^1.0.0",
    "nativescript-geolocation": "^4.3.1",
    "nativescript-localstorage": "^2.0.0",
    "nativescript-mapbox": "^4.4.0",
    "nativescript-theme-core": "^1.0.4",
    "nativescript-ui-sidedrawer": "^4.3.0",
    "nativescript-vue": "^2.0.0",
    "querystring": "^0.2.0",
    "request": "^2.88.0",
    "tns-core-modules": "^4.2.0",
    "vuex": "^3.0.1"
  },

What I am trying to do is to send geolocation information via https to a server. This info will be sent when the app is pushed in the background. I've followed the sample app https://github.com/NativeScript/sample-ios-background-execution for implementing a background delegate.

However, after running the command tns run ios --bundle --emulator, and press the home button to push the app into the background, the getCurrentLocation function returns nothing after a few seconds.

Here's an example of my console:

CONSOLE LOG file:///app/bundle.js:1685:20: 'Enter background'
...
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1359:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T18:55:52.234Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1359:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T18:55:52.234Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1359:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T18:55:52.249Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1359:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T18:55:58.255Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'
CONSOLE LOG file:///app/bundle.js:1712:20: 'Printing geolocation:'

I don't know what could be causing this? Could it be due to the geolocation module calling a native API, which is returning nothing?

Update I included a check to see if the app has permission (Ie geolocation.isEnabled()), and it always returns true, I then call getCurrentLocation. Same result.

RaisinBread42 avatar Nov 27 '18 19:11 RaisinBread42

@DimitarTodorov curious what could be causing this...I haven't experienced it but using it in the background may cause a problem?

jlooper avatar Nov 27 '18 22:11 jlooper

@jlooper that is my impression. I am running the app now. When I open the app again from the background, you can see the getCurrentLocation splurges out the location based on the ones that failed in the background.

So the console now looks like:

... few seconds after runs in background... 
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
CONSOLE LOG file:///app/bundle.js:1909:20: 'Printing geolocation:'
CONSOLE LOG 'Location Service enabled: true'
... opening the app again gives...
CONSOLE LOG file:///app/bundle.js:1483:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T22:23:36.058Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1483:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T22:23:36.058Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1483:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T22:23:36.058Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1483:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T22:23:36.058Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1483:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T22:23:36.058Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1483:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T22:23:36.058Z","ios":{}}'
CONSOLE LOG file:///app/bundle.js:1483:16: '{"latitude":37.785834,"longitude":-122.406417,"altitude":0,"horizontalAccuracy":5,"verticalAccuracy":-1,"speed":-1,"direction":-1,"timestamp":"2018-11-27T22:23:36.058Z","ios":{}}'
...

RaisinBread42 avatar Nov 27 '18 22:11 RaisinBread42

Do you have the iosAllowsBackgroundLocationUpdates option set to true in the .watchLocation() call options?

If possible, provide a sample app (https://play.nativescript.org/ works great for this) so we can review the code.

lini avatar Nov 28 '18 07:11 lini

@lini the playground doesn't allow me to add typescript files, which is what the background delegate is coded in.

Instead, I created a basic project and uploaded it as its own repo: https://github.com/RaisinBread42/nativescript-vue-background-geolocation, the instructions for running is in the README.md

RaisinBread42 avatar Nov 28 '18 12:11 RaisinBread42

@RaisinBread42 I tried your code on iOS device and what I found is that when the app is in the background the location service does not work (no icon on the status bar) or at least the the geolocation api can't use it as all attempts to get location fail with Error: Timeout while searching for location! Additional research should be made on the topic to find whether the plugin supports background mode on iOS so keep an eye on this issue.

zbranzov avatar Nov 29 '18 15:11 zbranzov

Hi

Any solution?

the location service in iOS doesn't run.

Mflm avatar Jan 09 '19 17:01 Mflm

Any progress with this issue @zbranzov ?

RaisinBread42 avatar Feb 23 '19 14:02 RaisinBread42

A few years ago I started developing a gps tracker in Cordova. A huge problem was geolocation updates in the background behaving unpredictable. Unfortunately it seems this is still an issue in 2019. I will monitor this issue and hope that a solution will be found (y)

de-raaf-media avatar Mar 29 '19 12:03 de-raaf-media

@zbranzov hi, so I have enabled iosAllowsBackgroundLocationUpdates this property, but seems didn't detect locations in background. can you please help?

karo-dc avatar Oct 14 '19 21:10 karo-dc