NativeScript / nativescript-geolocation

Geolocation plugin to use for getting current location, monitor movement, etc
Apache License 2.0
139 stars 76 forks source link

What's the correct way to get geolocation while the app is closed #233

Closed butaminas closed 5 years ago

butaminas commented 5 years ago

Which platform(s) does your issue occur on?

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

"dependencies": { "axios": "^0.19.0", "js-cookie": "^2.2.1", "nativescript-geolocation": "^5.1.0", "nativescript-theme-core": "^1.0.6", "nativescript-ui-sidedrawer": "~7.0.0", "nativescript-vue": "~2.4.0", "net": "^1.0.2", "rxjs": "~6.5.0", "tns-core-modules": "~6.0.0", "vuex": "^3.1.1" }, "devDependencies": { "@babel/core": "~7.1.0", "@babel/preset-env": "~7.1.0", "babel-loader": "~8.0.0", "nativescript-dev-webpack": "~1.0.0", "nativescript-vue-template-compiler": "~2.4.0", "node-sass": "^4.7.1", "vue-loader": "~15.4.0" },

Please, tell us how to recreate the issue in as much detail as possible.

I'm trying to get geolocation when the app is closed (not when suspended, but when closed). As I understand, I need to use enableLocationRequest with 'true' parameter but if I do this, the location is not being tracked at all.

What's the proper way to make location tracking even when the app is closed and will this also work on Android?

Is there any code involved?

For enabling geolocation:

geolocation.isEnabled().then(function (isEnabled) {
                    if (!isEnabled) {
                        geolocation.enableLocationRequest(true).then(function () { }, function (e) {
                            console.log("Error: " + (e.message || e));
                        });
                    }
                }, function (e) {
                    console.log("Error: " + (e.message || e));
                });

watcher for geolocation:

try {
                    let that = this;
                    this.watchIds.push(geolocation.watchLocation(
                        function (loc) {
                            if (loc) {
                                console.log("Location service: " + loc.timestamp)
                                that.$store.dispatch('usersStore/editUserGeo', {lat: loc.latitude, lng: loc.longitude})
                                that.locations.push(loc);
                            }
                        },
                        function (e) {
                            console.log("Error: " + e.message);
                        },
                        {
                            iosAllowsBackgroundLocationUpdates: true,
                            desiredAccuracy: Accuracy.high,
                        }));
                } catch (ex) {
                    console.log("Error: " + ex.message);
                }

By the way, I'm using VUE over here.

tbozhikov commented 5 years ago

Hi @butaminas, the code for enabling geolocation and watcher that you've posted above looks good, and that is the way to track while an app is suspended in iOS:

  1. Ensure you have the following keys in the info.plist. This is needed for Always authorization.
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>App requires GPS to track stuff</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>App requires GPS to track stuff</string>
  2. Call geolocation.enableLocationRequest(true) (true param stands for Always authorization)
  3. Use geolocation.watchLocation()

In Android, you can use geolocation.watchLocation() again, however, the background tracking should be implemented in a service class like the BackgroundService class in the TS demo of the plugin.

Regarding tracking location when the app is closed, I am not sure if it is possible in any OS at all.

Let me know if you have further questions.

butaminas commented 5 years ago

Regarding tracking location when the app is closed, I am not sure if it is possible in any OS at all.

What about apps like Google Maps? In privacy settings it has 3 options for tracking location:

  1. Never
  2. While using the App
  3. Always

The 3rd options (always) allows you to track location even when the app isn't in use:

Other apps will ask for access to your location even when the app isn't in use. When you allow an app to always use your location, iOS will remind you which apps are able to use your location after an app uses your location in the background.

Reference: https://support.apple.com/en-us/HT203033 ("Give apps permission to use your Location" section)

Is there any way to achieve this using nativescript and this geolocation plugin?

support[bot] commented 5 years ago

:wave: @butaminas, we use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please, use Stackoverflow to get help.

PhilippS93 commented 4 years ago

Regarding tracking location when the app is closed, I am not sure if it is possible in any OS at all.

What about apps like Google Maps? In privacy settings it has 3 options for tracking location:

  1. Never
  2. While using the App
  3. Always

The 3rd options (always) allows you to track location even when the app isn't in use:

Other apps will ask for access to your location even when the app isn't in use. When you allow an app to always use your location, iOS will remind you which apps are able to use your location after an app uses your location in the background.

Reference: https://support.apple.com/en-us/HT203033 ("Give apps permission to use your Location" section)

Is there any way to achieve this using nativescript and this geolocation plugin?

Anybody knows how to request the "Always" option (on Android) with this plugin?