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;
}
}
}