Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Android geolocation.getCurrentLocation gives error when no internet connection?? #108

Closed
simmy91 opened this issue Feb 8, 2018 · 9 comments

Comments

@simmy91
Copy link

simmy91 commented Feb 8, 2018

Make sure to check the demo app(s) for sample usage

I have, not come across anything similar

Make sure to check the existing issues in this repository

I have, not come across anything similar

If the demo apps cannot help and there is no issue for your problem, tell us about it

Location throws error when no internet connection??

Which platform(s) does your issue occur on?

  • Platform: Android 7.0 Nougat
  • Device: Huawei p8 Lite 2017 PRA-LX1

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

"nativescript": {
    "id": "testapp",
    "tns-ios": {
      "version": "3.4.1"
    },
    "tns-android": {
      "version": "3.4.1"
    }
  },
  "dependencies": {
    "@angular/animations": "~5.1.0",
    "@angular/common": "~5.1.0",
    "@angular/compiler": "~5.1.0",
    "@angular/core": "~5.1.0",
    "@angular/forms": "~5.1.0",
    "@angular/http": "~5.1.0",
    "@angular/platform-browser": "~5.1.0",
    "@angular/platform-browser-dynamic": "~5.1.0",
    "@angular/router": "~5.1.0",
    "nativescript-angular": "~5.1.0",
    "nativescript-fontawesome": "^1.0.0",
    "nativescript-geolocation": "^4.2.3",
    "nativescript-google-maps-sdk": "^2.4.3",
    "nativescript-localstorage": "^1.1.5",
    "nativescript-permissions": "^1.2.3",
    "nativescript-pro-ui": "^3.3.0",
    "nativescript-screen-orientation": "^2.0.0",
    "nativescript-theme-core": "~1.0.4",
    "nativescript-vibrate": "^2.0.1",
    "reflect-metadata": "~0.1.8",
    "rxjs": "~5.5.2",
    "tns-core-modules": "~3.4.0",
    "zone.js": "~0.8.18"
  },
  "devDependencies": {
    "babel-traverse": "6.4.5",
    "babel-types": "6.4.5",
    "babylon": "6.4.5",
    "lazy": "1.0.11",
    "nativescript-dev-typescript": "~0.6.0",
    "typescript": "~2.4.2"
  },

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

I check if the platform is Android by doing if(app.android) using import * as app from "tns-core-modules/application"; then I request location permission by doing permissions.requestPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, "") this is all fine. But then when I call geolocation.getCurrentLocation it gives an error saying "cannot enable location services" when i put the mobile in airplane mode or disabled mobile data/wifi. I need the geolocation to work offline and it should be getting the location viw the GPS signal and NOT internet right??

Is there any code involved?

@dlcole
Copy link

dlcole commented Feb 10, 2018

For what it's worth, I'm getting the same "cannot enable location services" error simply trying to run the geolocation tutorial project from tutsplus. I've updated everything to the latest versions, as per what I could find online, but still no joy, both on an Android emulator and device.

My point: the error may be more pervasive than lack of internet.

@simmy91
Copy link
Author

simmy91 commented Feb 10, 2018

You may need to modify the following file if the Android version is 6.0+

app/App_Resources/Android/app.gradle file:

android {
// other stuff here

project.ext {
googlePlayServicesVersion = "11.2.+"
}
}

This got my geolocation to work as I had trouble before with it not working at all.

@dlcole
Copy link

dlcole commented Feb 10, 2018

The addition to the app.gradle file solved the problem. Thanks!!

@simmy91
Copy link
Author

simmy91 commented Feb 11, 2018

@dlcole Your welcome! It is actually mentioned in the documentation https://github.com/NativeScript/nativescript-geolocation/blob/master/README.md towards the end.

@simmy91
Copy link
Author

simmy91 commented Feb 12, 2018

Does anyone still know why it don't work when the phone is in airplane mode/ no wifi or mobile data?

@NickIliev
Copy link

@dhandas180391 on what device have you tested the GPS in Airplane mode?
In my initial research, I've noticed that many Android users are having troubles getting GPS fix in Airplane mode depending on the device models (this and this)

@simmy91
Copy link
Author

simmy91 commented Feb 12, 2018

@NickIliev I am testing it on Huawei p8 Lite 2017 PRA-LX1

According to this: https://www.google.bg/search?rlz=1C1CHBF_enBG781BG781&biw=1920&bih=989&ei=wGqBWtLcKc2AgQailLzIDg&q=android+mobile+get+gps+location+in+airplane+mode&oq=android+mobile+get+gps+location+in+airplane+mode&gs_l=psy-ab.3...710175.717262.0.717358.40.40.0.0.0.0.117.2697.39j1.40.0....0...1.1.64.psy-ab..0.39.2640...0j0i131k1j0i22i10i30k1j0i22i30k1j33i22i29i30k1j33i21k1j33i160k1.0.tmtJ6MyB-S4

it should work, maybe this is limited only on the nativescript-geolocation??

For what I am doing I need to be able on an Android device to get the location even when in airplane mode, it works fine without wifi/mobile data now after more testing.

@manojdcoder
Copy link
Contributor

It seems the plugin still works keeping internet / wifi off. But when it's in Airplane mode (no wifi neither cellular network) but still GPS is on, it fails to detect location.

The issue seems to be the check made at these lines below. In Airplane mode the location settings check fails but still location manager is capable of fetching the location. If I bypass that check and let it proceed to location manager, it returns me the right location just like Google Map does. I suggest reopening this ticket and probably discuss a way to handle this scenario.

https://github.com/NativeScript/nativescript-geolocation/blob/master/src/geolocation.android.ts#L179-L194

if (typeof ex.getStatusCode === "function" &&
                            ex.getStatusCode() === com.google.android.gms.common.api.CommonStatusCodes.RESOLUTION_REQUIRED) {

                        try {
                            // cache resolve and reject callbacks in order to call them
                            // on REQUEST_ENABLE_LOCATION Activity Result
                            _onEnableLocationSuccess = resolve;
                            _onEnableLocationFail = reject;
                            ex.startResolutionForResult(androidAppInstance.foregroundActivity, REQUEST_ENABLE_LOCATION);
                        } catch (sendEx) {
                            // Ignore the error.
                            resolve();
                        }
                    } else {
                        reject(new Error("Cannot enable the location service"));
                    }

@manojdcoder
Copy link
Contributor

To be precise, same issue is described in detail here and I believe this must be handled by the plugin.

android/location-samples/issues/47

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants