0

I wrote some Code with nativescript and angular to watch the location.

But even though GPS on my device is switched on, geolocation.isEnabled() says false.

Plus, when running geolocation.watchLocation(..) my device only opens Location Settings. And comes back with the Error Message:

Error: Location service is not enabled or using it is not granted.

Does anybody know the reason why my app can't use location?

My app.compoment.html:

<GridLayout columns="*,*" rows="auto,auto,auto">
    <Label text="Geolocation" class="title" row="0" colSpan="2"></Label>
    <Button row="1" col="0" text="Start Tracking" (tap)="startTracking()"></Button>
    <Button row="1" col="1" text="Stop Tracking" (tap)="stopTracking()"></Button>
    <Label row="2" col="0" text="Latitude: {{latitude}}"></Label>
    <Label row="2" col="1" text="Longitude: {{longitude}}"></Label>
</GridLayout>

My app.component.ts:

@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {

latitude = "42";
longitude = "5";
watchId = 0;

public startTracking() {
    console.log("Start Tracking");

    /* Check if GPS is enabled. */
    if (geolocation.isEnabled()) {
        console.log("GPS is enabled.");
    } else {
        console.log("GPS is disabled.");
    }

    /* Get Location */
    this.watchId = geolocation.watchLocation(function (loc) {
            if (loc) {
                console.log("Current location is: " + loc);
            }
        }, function (e) {
            console.log("Error: " + e.message);
        },
        {desiredAccuracy: enums.Accuracy.any, updateDistance: 10, minimumUpdateTime: 1000});
}

public stopTracking() {
    console.log("Stop Tracking");
    if (this.watchId) {
        geolocation.clearWatch(this.watchId);
        this.watchId = 0;
    }
}
}

1 Answer 1

1

Have you tried to explicitly set the permissions for geolocation service on your device?

public enableLocationTap() { 
    if (!geolocation.isEnabled()) {
        geolocation.enableLocationRequest();
    }
}

// in page.html
<StackLayout>
    <Button text="enable Location" (tap)="enableLocationTap()"></Button>
</StackLayout>

After triggering the button the app should show a permission popup with allow/cancel style message.

Sign up to request clarification or add additional context in comments.

5 Comments

I will try your solution
Isn't there a way to grant the app these permissions automatically on the app install? For an example, when installing pokemon go, it already says it will have access to your location services if you install this app, and inside the app you don't have to request for it.
Yes, you can - Permission for all verison of Android below Nougat (Android 7) are requested ni AndroidManifest.xml. When you need to explicitly grant permissions (like the Pokemon Go example or for all Android-7 pps) you can do it in app.js before the application.start() method. You can use for example nativescript-permissions plugin
Thanks, yeah, found the manifest in app/App_Resources/Android/AndroidManifest.xml. I can see that uses-permission only allows internet and read/write external storage, but on the nativescript location article they say they add this permission. Seems its done on the fly, on manifest conversion to android code. If anyone else is wondering why their app is getting that error - try to delete the app on your phone. Once you include the nativescript-geolocation from npm, you have to completely rebuild the app. Now in settings I can see in App info, that my app now can access precise location :)
Oh, and I kept getting the error Error: API key not found. Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key" /> is in the <application> element of AndroidManifest.xml. Dunno why, but now it's not enough to put your key in app/App_Resources/Android/values/nativescript_google_maps_api.xml, so I did what error suggested - put that element as a first child of <application> element and it started working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.